diff --git a/utils/compress.js b/utils/compress.js new file mode 100644 index 0000000..be17c9d --- /dev/null +++ b/utils/compress.js @@ -0,0 +1,31 @@ +const hbjs = require('handbrake-js'); +const events = require('events'); + +module.exports = function(input, output) { + let eventEmitter = new events.EventEmitter(); + + if (!input) eventEmitter.emit('error', 'Require an input file! (If you see this message please send a feedback to the dev about it.)'); + if (!output) eventEmitter.emit('error', 'Require an output parameter! (If you see this message please send a feedback to the dev about it.)'); + + const options = { + input: input, + output: output, + preset: 'Web/Discord Tiny 5 Minutes 240p30' + }; + + let handbrake = hbjs.spawn(options); + + handbrake.on('progress', progress => { + eventEmitter.emit('progress', progress); + }); + + handbrake.on('error', err => { + console.log(err); + eventEmitter.emit('error', err); + }); + + handbrake.on('end', async function () { + eventEmitter.emit('end', output); + }); + return eventEmitter; +}; diff --git a/utils/download.js b/utils/download.js new file mode 100644 index 0000000..0a088d4 --- /dev/null +++ b/utils/download.js @@ -0,0 +1,26 @@ +const fs = require('fs'); +const youtubedl = require('youtube-dl'); + +module.exports = function (url, option, output) { + return new Promise(function(resolve, reject) { + if (!url) reject('Require a url!'); + if (!output) reject('Require an output parameter! (If you see this message please send a feedback to the dev about it.)'); + + if (option != null) option.push('--rm-cache-dir'); + else option = ['--rm-cache-dir']; + + const video = youtubedl(url, option); + + video.on('error', function error(err) { + console.log(err.toString()); + reject(err.toString()); + }); + + video.pipe(fs.createWriteStream(output)); + + video.on('end', function() { + resolve(output); + }); + }); +}; +