Compress video if bigger than 8 MB

This commit is contained in:
loicbersier 2019-01-19 17:23:45 +01:00
parent f553115bf1
commit 4ef34bc489

View file

@ -1,6 +1,7 @@
const { Command } = require('discord-akairo'); const { Command } = require('discord-akairo');
const fs = require('fs'); const fs = require('fs');
const youtubedl = require('youtube-dl'); const youtubedl = require('youtube-dl');
const hbjs = require('handbrake-js');
const { fbuser, fbpasswd } = require('../../config.json'); const { fbuser, fbpasswd } = require('../../config.json');
class DownloadCommand extends Command { class DownloadCommand extends Command {
@ -26,24 +27,57 @@ class DownloadCommand extends Command {
async exec(message, args) { async exec(message, args) {
let link = args.link; let link = args.link;
let big = false;
if (link.includes('http') || link.includes('www')) { if (link.includes('http') || link.includes('www')) {
message.channel.send('Downloading <a:loadingmin:527579785212329984>').then(msg => { let video = youtubedl(link, [`--username=${fbuser}`, `--password=${fbpasswd}`, '--max-filesize', '8M']);
video.on('end', function () {
msg.delete();
});
});
let video = youtubedl(link, [`--username=${fbuser}`, `--password=${fbpasswd}`]);
video.pipe(fs.createWriteStream('./video.mp4')); video.pipe(fs.createWriteStream('./video.mp4'));
video.on('error', function error(err) { video.on('error', function error(err) {
console.log('error 2:', err); console.log('error 2:', err);
message.channel.send('An error has occured, I can\'t download from the link you provided.'); message.channel.send('An error has occured, I can\'t download from the link you provided.');
}); });
video.on('info', function(info) {
console.log('Download started');
console.log('filename: ' + info.filename);
console.log('size: ' + info.size);
if (info.size >= 8000000) {
big = true;
}
});
video.on('end', function () { video.on('end', function () {
message.delete(); if (!big) {
message.channel.send(`Downloaded by ${message.author.username}`, { files: ['./video.mp4'] }) message.delete();
.catch(() => message.channel.send('File too big')); return message.channel.send(`Downloaded by ${message.author.username}`, { files: ['./video.mp4'] })
.catch(() => message.channel.send('File too big'));
}
const options = {
input: 'video.mp4',
output: 'videoReady.mp4',
preset: 'General/Gmail Small 10 Minutes 288p30'
};
hbjs.spawn(options)
.on('error', err => {
message.channel.send('An error has occured while compressing the video');
console.error(err);
})
.on('progress', progress => {
console.log(
'Percent complete: %s, ETA: %s',
progress.percentComplete,
progress.eta
);
})
.on('end', function () {
message.delete();
message.channel.send(`Downloaded by ${message.author.username}`, { files: ['./videoReady.mp4'] })
.catch(() => message.channel.send('File too big'));
});
message.channel.send('Downloading <a:loadingmin:527579785212329984>').then(msg => {
video.on('end', function () {
msg.delete();
});
});
}); });
} else { } else {