From 89f1d056fc2530a2910a3e5018352ef72a0be8eb Mon Sep 17 00:00:00 2001 From: loicbersier Date: Wed, 17 Jun 2020 18:42:52 +0200 Subject: [PATCH] Use the new download and compress files --- commands/fun/midify.js | 54 +++++--------- commands/fun/vidshittifier.js | 15 ++-- commands/fun/ytp.js | 34 +++------ commands/utility/download.js | 133 ++++++++++++++++++++++++++++++++++ 4 files changed, 170 insertions(+), 66 deletions(-) create mode 100644 commands/utility/download.js diff --git a/commands/fun/midify.js b/commands/fun/midify.js index 548f5130..60912713 100644 --- a/commands/fun/midify.js +++ b/commands/fun/midify.js @@ -1,10 +1,8 @@ const { Command } = require('discord-akairo'); const util = require('util'); const exec = util.promisify(require('child_process').exec); -const youtubedl = require('youtube-dl'); +const downloader = require('../../utils/download'); const os = require('os'); -const filetype = require('file-type'); -const fs = require('fs'); const ffmpeg = require('fluent-ffmpeg'); @@ -61,39 +59,25 @@ class midifyCommand extends Command { let loadingmsg = await message.channel.send('Processing (this can take some time) '); if (url) { - return youtubedl.exec(url, ['-o', input], {}, async function(err) { - - if (err) { - console.error(err); + downloader(url, null, input) + .catch((err) => { loadingmsg.delete(); - return message.channel.send('An error has occured, I can\'t download from the link you provided.'); - } - let ext = 'mp4'; - - 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 - ffmpeg() - .input(input) - .output(input2) - .on('end', () => { - midify(); - }) - .on('error', (err, stdout, stderr) => { - console.error(`${err}\n${stdout}\n${stderr}`); - return message.channel.send('Oh no! an error has occured during the conversion, are you sure it is a valid file?'); - }) - .run(); - }); + return message.channel.send(err, { code: true }); + }) + .then(output => { + // Convert to wav + ffmpeg() + .input(output) + .output(input2) + .on('end', () => { + midify(); + }) + .on('error', (err, stdout, stderr) => { + console.error(`${err}\n${stdout}\n${stderr}`); + return message.channel.send('Oh no! an error has occured during the conversion, are you sure it is a valid file?'); + }) + .run(); + }); } else { return message.channel.send('You need a valid video link!'); } diff --git a/commands/fun/vidshittifier.js b/commands/fun/vidshittifier.js index 89a11d34..8a4c378c 100644 --- a/commands/fun/vidshittifier.js +++ b/commands/fun/vidshittifier.js @@ -1,7 +1,7 @@ const { Command } = require('discord-akairo'); const util = require('util'); const exec = util.promisify(require('child_process').exec); -const youtubedl = require('youtube-dl'); +const downloader = require('../../utils/download'); const os = require('os'); class vidshittifierCommand extends Command { @@ -56,15 +56,14 @@ class vidshittifierCommand extends Command { let loadingmsg = await message.channel.send('Processing '); if (url) { - return youtubedl.exec(url, ['--format=mp4', '-o', input], {}, function(err) { - if (err) { - console.error(err); + downloader(args.link, null, input) + .catch((err) => { loadingmsg.delete(); - return message.channel.send('An error has occured, I can\'t download from the link you provided. Try again!'); - } else { + return message.channel.send(err, { code: true }); + }) + .then(() => { shittifie(); - } - }); + }); } else { return message.channel.send('You need a valid video link! Try again!'); } diff --git a/commands/fun/ytp.js b/commands/fun/ytp.js index cab624ca..35f9c15e 100644 --- a/commands/fun/ytp.js +++ b/commands/fun/ytp.js @@ -2,7 +2,7 @@ const { Command } = require('discord-akairo'); const YTPGenerator = require('ytpplus-node'); const os = require('os'); const fs = require('fs'); -const youtubedl = require('youtube-dl'); +const downloader = require('../../utils/download'); const md5File = require('md5-file'); const ytpHash = require('../../models').ytpHash; @@ -136,29 +136,17 @@ class ytpCommand extends Command { } 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) { - console.log(output); - if (err) { - console.error(err.toString()); + return downloader(url, ['--format=mp4'], `./asset/ytp/userVid/${message.id}.mp4`) + .catch((err) => { loadingmsg.delete(); - if (err.toString().includes('HTTP Error 429') || err.toString().includes('HTTP Error 403')) { - 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 { - return message.channel.send('An error has occured, I can\'t download from the link you provided. Is it an mp4?'); - } - } 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`); + return message.channel.send(err, { code: true }); + }) + .then(async output => { + const hash = md5File.sync(output); const ytphash = await ytpHash.findOne({where: {hash: hash}}); if (ytphash) { - fs.unlinkSync(`./asset/ytp/userVid/${message.id}.mp4`); + fs.unlinkSync(output); loadingmsg.delete(); return message.reply('This video is a duplicate... Not adding.'); } else { @@ -175,9 +163,9 @@ class ytpCommand extends Command { }); 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 { loadingmsg.delete(); return message.channel.send('You need a valid video link!'); diff --git a/commands/utility/download.js b/commands/utility/download.js new file mode 100644 index 00000000..8ba6867e --- /dev/null +++ b/commands/utility/download.js @@ -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 '); + 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; \ No newline at end of file