| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- import { Meteor } from 'meteor/meteor';
- import { Mongo } from 'meteor/mongo';
- import { check, Match } from 'meteor/check';
-
- export const Converted = new Mongo.Collection('links');
-
- const YTExp = /http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-_]*)/;
-
- if (Meteor.isServer) {
- Meteor.publish('links', function linksPublication() {
- return Converted.find();
- });
- }
-
- const convert = (url, filename, id) => {
- if (Meteor.isServer) {
- console.log('Converting ...');
- const fs = Meteor.npmRequire("fs");
- const ytdl = Meteor.npmRequire('ytdl-core');
- const path = Meteor.npmRequire('path');
- const ffmpeg = Meteor.npmRequire('fluent-ffmpeg');
- const filePath = path.resolve('/home/kod3/projects/SoundWave/uploaded/', filename)
- const videoPath = filePath + '.mp4';
- const audioPath = filePath + '.mp3';
- ytdl.getInfo(url, {}, Meteor.bindEnvironment((err, info) => {
- console.info(info ? 'ok' : 'ko');
- if (err) {
- console.log(url);
- console.error(err);
- Converted.update({ _id: id }, { $set: {
- error_video: 'Video is unreachable',
- error_audio: 'Video is unreachable',
- } });
- } else {
- const totalSeconds = info ? info.length_seconds : 0;
- const dl = ytdl(url);
- dl.pipe(fs.createWriteStream(videoPath));
- ffmpeg(dl)
- .noVideo()
- .on('end', Meteor.bindEnvironment(() => {
- Converted.update({ _id: id }, { $set: { audio: filename + '.mp3', audio_progress: 100 } });
- }))
- .on('progress', Meteor.bindEnvironment((params) => {
- const time = moment(new Date(...[0, 0, 0, ...params.timemark.split(':')]));
- const seconds = time.hours() * 3600 + time.minutes() * 60 + time.seconds();
- let percent = ((seconds / totalSeconds) * 100).toFixed(0);
- Converted.update({ _id: id }, { $set: { audio_progress: percent } });
- }))
- .on('error', Meteor.bindEnvironment((err) => {
- console.error('MP3 encoding error: ' + err.message);
- Converted.update({ _id: id }, { $set: { error_audio: 'Audio cannot be extracted' } });
- }))
- .format('mp3')
- .output(fs.createWriteStream(audioPath))
- .run();
- dl.on('info', (info, format) => console.log);
- dl.on('response', Meteor.bindEnvironment((res) => {
- const totalSize = res.headers['content-length'];
- let dataRead = 0;
- res.on('data', Meteor.bindEnvironment((data) => {
- dataRead += data.length;
- let percent = dataRead / totalSize;
- Converted.update({ _id: id }, { $set: { video_progress: (percent * 100).toFixed(0) } });
- }));
- res.on('end', Meteor.bindEnvironment(() => {
- Converted.update({ _id: id }, { $set: { video: filename + '.mp4', video_progress: 100 } });
- }));
- res.on('error', Meteor.bindEnvironment((err) => {
- console.error('Error: ' + err.message)
- Converted.update({ _id: id }, { $set: { error_video: 'Video cannot be saved' } });
- }));
- }));
- }
- }));
- } else return false;
- };
-
- Meteor.methods({
-
- 'links.add'({video, type}) {
- check(video.url, Match.Where(url => YTExp.test(url)));
- check(type, Match.Where(t => ['audio'].includes(t)));
-
- let converted = Converted.findOne({
- url: video.url,
- error_audio: { $ne: null },
- error_video: { $ne: null },
- });
-
- if (!converted) {
- const filename = video.title.replace(/ /g, '_');
- Converted.remove({url: video.url});
- var inserted_id = Converted.insert({
- id: video.id,
- url: video.url,
- title: video.title,
- video: '',
- video_progress: 0,
- error_video: '',
- audio: '',
- audio_progress: 0,
- error_audio: '',
- createdAt: new Date()
- });
- convert(video.url, filename, inserted_id);
- }
- },
-
- });
|