| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- 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 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: audioPath, 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', (err) => { console.error('MP3 encoding error: ' + err.message); })
- .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: videoPath, video_progress: 100 } });
- }));
- res.on('error', (err) => { console.error('Error: ' + err.message) });
- }));
- }
- }));
- } 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
- });
-
- if (!converted) {
- const filename = video.title.replace(/ /g, '_');
- var inserted_id = Converted.insert({
- id: video.id,
- url: video.url,
- title: video.title,
- video: '',
- video_progress: 0,
- audio: '',
- audio_progress: 0,
- createdAt: new Date()
- });
- convert(video.url, filename, inserted_id);
- }
- },
-
- });
|