Moved downloading of video and compressing into its own file
This commit is contained in:
parent
5b5f2bfb23
commit
4dd1177379
2 changed files with 57 additions and 0 deletions
31
utils/compress.js
Normal file
31
utils/compress.js
Normal file
|
@ -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;
|
||||
};
|
26
utils/download.js
Normal file
26
utils/download.js
Normal file
|
@ -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);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
Loading…
Reference in a new issue