Youtube music and video downloader

converter.js 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { Meteor } from 'meteor/meteor';
  2. import { Converted } from './links';
  3. _ = lodash;
  4. const bindHandlers = (ressource, type, path, filename, id, url, ) => {
  5. let capType = _.capitalize(type);
  6. ressource.on('unavailable', Meteor.bindEnvironment(err => {
  7. console.error('KO', url, err);
  8. Converted.update({ _id: id }, { $set: { [`error_${type}`]: `${capType} is unreachable` } });
  9. }))
  10. .on('available', info => {
  11. console.info('OK');
  12. ressource.download(path);
  13. })
  14. .on('progress', Meteor.bindEnvironment(percent => {
  15. Converted.update({ _id: id }, { $set: { [`${type}_progress`]: percent } })
  16. }))
  17. .on('error', Meteor.bindEnvironment(err => {
  18. console.error(`${capType} download error: ${err.message}`);
  19. Converted.update({ _id: id }, { $set: { [`error_${type}`]: `${capType} cannot be saved` } });
  20. }))
  21. .on('end', Meteor.bindEnvironment(() => {
  22. console.info(`${capType} download complete for ${url}`);
  23. Converted.update({ _id: id }, { $set: { [type]: filename, [`${type}_progress`]: 100 } });
  24. }));
  25. };
  26. const convertAudio = (url, filename, id) => {
  27. if (Meteor.isServer) {
  28. import { AudioConverter } from '../../server/Converter';
  29. import path from 'path';
  30. const filePath = path.resolve(process.env.UPLOAD_DIR, filename);
  31. console.log(`Converting ${url} to MP3...`);
  32. let audio_dl = new AudioConverter(url);
  33. bindHandlers(audio_dl, 'audio', filePath + '.mp3', filename + '.mp3', id, url);
  34. }
  35. };
  36. const convertVideo = (url, filename, id) => {
  37. if (Meteor.isServer) {
  38. import { VideoConverter } from '../../server/Converter';
  39. import path from 'path';
  40. const filePath = path.resolve(process.env.UPLOAD_DIR, filename);
  41. console.log(`Converting ${url} to MP4 ...`);
  42. let video_dl = new VideoConverter(url);
  43. bindHandlers(video_dl, 'video', filePath + '.mp4', filename + '.mp4', id, url);
  44. }
  45. };
  46. export { convertAudio, convertVideo };