Use the new download and compress files

This commit is contained in:
loicbersier 2020-06-17 18:42:52 +02:00
parent eb6dad4c90
commit 89f1d056fc
4 changed files with 170 additions and 66 deletions

View file

@ -1,10 +1,8 @@
const { Command } = require('discord-akairo'); const { Command } = require('discord-akairo');
const util = require('util'); const util = require('util');
const exec = util.promisify(require('child_process').exec); const exec = util.promisify(require('child_process').exec);
const youtubedl = require('youtube-dl'); const downloader = require('../../utils/download');
const os = require('os'); const os = require('os');
const filetype = require('file-type');
const fs = require('fs');
const ffmpeg = require('fluent-ffmpeg'); const ffmpeg = require('fluent-ffmpeg');
@ -61,29 +59,15 @@ class midifyCommand extends Command {
let loadingmsg = await message.channel.send('Processing (this can take some time) <a:loadingmin:527579785212329984>'); let loadingmsg = await message.channel.send('Processing (this can take some time) <a:loadingmin:527579785212329984>');
if (url) { if (url) {
return youtubedl.exec(url, ['-o', input], {}, async function(err) { downloader(url, null, input)
.catch((err) => {
if (err) {
console.error(err);
loadingmsg.delete(); loadingmsg.delete();
return message.channel.send('An error has occured, I can\'t download from the link you provided.'); return message.channel.send(err, { code: true });
} })
let ext = 'mp4'; .then(output => {
if (fs.existsSync(input)) {
ext = await filetype.fromFile(input);
ext = ext.ext; // This look stupid but hey, it work
if (ext == '3gp') ext = 'mp4'; // Change 3gp file extension to mp4 so discord show the video ( and to stop people from complaining )
fs.renameSync(input, `${input}.${ext}`);
} else if (fs.existsSync(`${input}.mkv`)) { // If it can't find the video assume it got merged and end with mkv
fs.renameSync(`${input}.mkv`, `${input}.mp4`); // Discord play mkv just fine but it need to end with mp4
}
input = `${os.tmpdir()}/${message.id}.${ext}`;
// Convert to wav // Convert to wav
ffmpeg() ffmpeg()
.input(input) .input(output)
.output(input2) .output(input2)
.on('end', () => { .on('end', () => {
midify(); midify();

View file

@ -1,7 +1,7 @@
const { Command } = require('discord-akairo'); const { Command } = require('discord-akairo');
const util = require('util'); const util = require('util');
const exec = util.promisify(require('child_process').exec); const exec = util.promisify(require('child_process').exec);
const youtubedl = require('youtube-dl'); const downloader = require('../../utils/download');
const os = require('os'); const os = require('os');
class vidshittifierCommand extends Command { class vidshittifierCommand extends Command {
@ -56,14 +56,13 @@ class vidshittifierCommand extends Command {
let loadingmsg = await message.channel.send('Processing <a:loadingmin:527579785212329984>'); let loadingmsg = await message.channel.send('Processing <a:loadingmin:527579785212329984>');
if (url) { if (url) {
return youtubedl.exec(url, ['--format=mp4', '-o', input], {}, function(err) { downloader(args.link, null, input)
if (err) { .catch((err) => {
console.error(err);
loadingmsg.delete(); loadingmsg.delete();
return message.channel.send('An error has occured, I can\'t download from the link you provided. Try again!'); return message.channel.send(err, { code: true });
} else { })
.then(() => {
shittifie(); shittifie();
}
}); });
} else { } else {
return message.channel.send('You need a valid video link! Try again!'); return message.channel.send('You need a valid video link! Try again!');

View file

@ -2,7 +2,7 @@ const { Command } = require('discord-akairo');
const YTPGenerator = require('ytpplus-node'); const YTPGenerator = require('ytpplus-node');
const os = require('os'); const os = require('os');
const fs = require('fs'); const fs = require('fs');
const youtubedl = require('youtube-dl'); const downloader = require('../../utils/download');
const md5File = require('md5-file'); const md5File = require('md5-file');
const ytpHash = require('../../models').ytpHash; const ytpHash = require('../../models').ytpHash;
@ -136,29 +136,17 @@ class ytpCommand extends Command {
} }
if (url) { if (url) {
return youtubedl.exec(url, ['--rm-cache-dir', '--no-playlist', '--max-filesize', '50m', '--format=mp4', '-o', `./asset/ytp/userVid/${message.id}.mp4`], {}, async function(err, output) { return downloader(url, ['--format=mp4'], `./asset/ytp/userVid/${message.id}.mp4`)
console.log(output); .catch((err) => {
if (err) {
console.error(err.toString());
loadingmsg.delete(); loadingmsg.delete();
if (err.toString().includes('HTTP Error 429') || err.toString().includes('HTTP Error 403')) { return message.channel.send(err, { code: true });
return message.channel.send('An error has occured, I can\'t download from the link you provided because the website has blocked the bot. Please try again later or upload that video as mp4 on another website.'); })
} else { .then(async output => {
return message.channel.send('An error has occured, I can\'t download from the link you provided. Is it an mp4?'); const hash = md5File.sync(output);
}
} else {
if (output[2]) {
if (output[2].includes('File is larger than max-filesize')) {
loadingmsg.delete();
return message.channel.send(output[2]);
}
}
const hash = md5File.sync(`./asset/ytp/userVid/${message.id}.mp4`);
const ytphash = await ytpHash.findOne({where: {hash: hash}}); const ytphash = await ytpHash.findOne({where: {hash: hash}});
if (ytphash) { if (ytphash) {
fs.unlinkSync(`./asset/ytp/userVid/${message.id}.mp4`); fs.unlinkSync(output);
loadingmsg.delete(); loadingmsg.delete();
return message.reply('This video is a duplicate... Not adding.'); return message.reply('This video is a duplicate... Not adding.');
} else { } else {
@ -175,8 +163,8 @@ class ytpCommand extends Command {
}); });
loadingmsg.delete(); loadingmsg.delete();
return message.reply(`Video sucessfully added to the pool! There is now ${mp4.length} videos`); return message.reply(`Video successfully added to the pool! There is now ${mp4.length} videos`);
}
}); });
} else { } else {
loadingmsg.delete(); loadingmsg.delete();

View file

@ -0,0 +1,133 @@
const { Command } = require('discord-akairo');
const downloader = require('../../utils/download');
const compress = require('../../utils/compress');
const os = require('os');
const fs = require('fs');
class DownloadCommand extends Command {
constructor() {
super('download2', {
aliases: ['download2', 'dl2'],
category: 'utility',
clientPermissions: ['SEND_MESSAGES', 'EMBED_LINKS', 'ATTACH_FILES'],
args: [
{
id: 'link',
type: 'string',
prompt: {
start: 'Send the link of which video you want to download',
}
},
{
id: 'caption',
type: 'string',
match: 'rest'
},
{
id: 'spoiler',
match: 'flag',
flag: ['--spoil', '--spoiler', '-s']
}
],
description: {
content: 'Download videos from different website from the link you provided, use "-s" to make the vid a spoiler',
usage: '[link] [caption]',
examples: ['https://www.youtube.com/watch?v=6n3pFFPSlW4 Look at this funny gnome']
}
});
}
async exec(message, args) {
let loadingmsg = await message.channel.send('Downloading <a:loadingmin:527579785212329984>');
let filename = `${message.id}_video`;
if (args.spoiler) {
filename = `SPOILER_${message.id}_video`;
}
const Embed = this.client.util.embed()
.setColor(message.member ? message.member.displayHexColor : 'NAVY')
.setAuthor(`Downloaded by ${message.author.username}`, message.author.displayAvatarURL(), args.link)
.setDescription(args.caption ? args.caption : '')
.setFooter(`You can get the original video by clicking on the "downloaded by ${message.author.username}" message!`);
downloader(args.link, null, `${os.tmpdir()}/${filename}.mp4`)
.catch((err) => {
return message.channel.send(err, { code: true });
})
.then(async (output) => {
loadingmsg.delete();
let file = fs.statSync(output);
let fileSize = file.size / 1000000.0;
if (fileSize > 8) {
let compressEmbed = this.client.util.embed()
.setColor(message.member ? message.member.displayHexColor : 'NAVY')
.setTitle('This one will need compression!')
.setDescription('Starting compression now!')
.setFooter('Want it to go faster? Donate to the dev with the donate command, so i can get a better server and do it faster!');
let compressmsg = await message.channel.send(compressEmbed);
let handbrake = compress(output, `${os.tmpdir()}/${filename}compressed.mp4`);
let percentComplete;
let eta;
handbrake.on('progress', progress => {
percentComplete = progress.percentComplete;
eta = progress.eta;
});
// Every 5 seconds update the compress message with the %
let editmsg = setInterval(() => {
compressEmbed.setDescription(`Ready in ${eta === '' ? 'soon enough' : eta}. ${percentComplete}% complete.`);
compressmsg.edit(compressEmbed);
}, 5000);
handbrake.on('err', (err) => {
clearInterval(editmsg);
compressmsg.delete();
return message.channel.send(err, { code: true });
});
handbrake.on('end', (output) => {
clearInterval(editmsg);
compressmsg.delete();
file = fs.statSync(output);
fileSize = file.size / 1000000.0;
if (fileSize > 8) return message.channel.send('End results is too big for discord.');
return message.channel.send({embed: Embed, files: [output]})
.catch(err => {
console.error(err);
return message.channel.send('File too big');
})
.then(() => {
// Delete file after it has been sent
fs.unlinkSync(output);
message.delete();
});
});
} else {
return message.channel.send({embed: Embed, files: [output]})
.catch(err => {
console.error(err);
return message.channel.send('File too big');
})
.then(() => {
// Delete file after it has been sent
fs.unlinkSync(output);
message.delete();
});
}
});
}
}
module.exports = DownloadCommand;