From 7d0ae36b63d4538adb19d874ab6e0962070c164a Mon Sep 17 00:00:00 2001 From: loicbersier Date: Thu, 16 Jul 2020 09:20:24 +0200 Subject: [PATCH] Use fluent-ffmpeg --- commands/fun/vidshittifier.js | 97 ---------------------------------- commands/fun/vidshittyfier.js | 99 +++++++++++++++++++++++++++++++++++ commands/utility/gif2vid.js | 75 +++++++++++++------------- commands/utility/vid2gif.js | 87 ++++++++++++------------------ 4 files changed, 168 insertions(+), 190 deletions(-) delete mode 100644 commands/fun/vidshittifier.js create mode 100644 commands/fun/vidshittyfier.js diff --git a/commands/fun/vidshittifier.js b/commands/fun/vidshittifier.js deleted file mode 100644 index 6703b296..00000000 --- a/commands/fun/vidshittifier.js +++ /dev/null @@ -1,97 +0,0 @@ -const { Command } = require('discord-akairo'); -const util = require('util'); -const exec = util.promisify(require('child_process').exec); -const downloader = require('../../utils/download'); -const os = require('os'); - -class vidshittifierCommand extends Command { - constructor() { - super('vidshittifier', { - aliases: ['vidshittifier', 'vs', 'shittifier', 'vid2shit', 'v2s'], - category: 'fun', - clientPermissions: ['SEND_MESSAGES', 'ATTACH_FILES'], - args: [ - { - id: 'link', - type: 'string', - }, - { - id: 'compression', - type: 'integer' - } - ], - description: { - content: 'Compress your videos and lower their quality!', - usage: '[link to video] [compression ( 1, 2 or 3)]', - examples: [''] - } - }); - } - - async exec(message, args) { - let Attachment = (message.attachments).array(); - let url = args.link; - // Get attachment link - if (Attachment[0] && !args.link) { - url = Attachment[0].url; - } - - let input = `${os.tmpdir()}/${message.id}.mp4`; - let input2 = `${os.tmpdir()}/tmp${message.id}.mp4`; - let output = `${os.tmpdir()}/Shittified${message.id}.mp4`; - - let compression; - let audioCompression; - - if (args.compression === 1) { - compression = '50k'; - audioCompression = '100k'; - } else { - compression = '30k'; - audioCompression = '60k'; - } - - let option = `-b:v ${compression} -b:a ${audioCompression}`; - - let loadingmsg = await message.channel.send('Processing '); - - if (url) { - downloader(args.link, null, input) - .on('error', (err) => { - loadingmsg.delete(); - return message.channel.send(err, { code: true }); - }) - .on('end', () => { - shittifie(); - }); - } else { - return message.channel.send('You need a valid video link! Try again!'); - } - - function shittifie() { - // reduce video resolution - exec(`ffmpeg -i ${input} -vf "scale=iw/4:ih/4" ${input2}`) - .then(() => { - // upscale video and change bitrate - exec(`ffmpeg -i ${input2} ${option} -vf "scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2" -vcodec libx264 -r 15 -f mp4 ${output}`) - .then(() => { - loadingmsg.delete(); - message.delete(); - return message.channel.send({files: [output]}) - .catch(err => { - console.error(err); - loadingmsg.delete(); - return message.channel.send('On no! an error just occured! Maybe the file is too big?'); - }); - }) - .catch(err => { - console.error(err); - loadingmsg.delete(); - return message.channel.send('Oh no! an error just occured! We don\'t know what causes this error yet, so let us know!'); - }); - }); - } - } -} - -module.exports = vidshittifierCommand; \ No newline at end of file diff --git a/commands/fun/vidshittyfier.js b/commands/fun/vidshittyfier.js new file mode 100644 index 00000000..1eb8d4cf --- /dev/null +++ b/commands/fun/vidshittyfier.js @@ -0,0 +1,99 @@ +const { Command } = require('discord-akairo'); +const os = require('os'); +const ffmpeg = require('fluent-ffmpeg'); +const attachment = require('../../utils/attachment'); +const downloader = require('../../utils/download'); + +class vidshittyfierCommand extends Command { + constructor() { + super('vidshittyfier', { + aliases: ['vidshittyfier', 'vs', 'shittyfier', 'vid2shit', 'v2s'], + category: 'fun', + clientPermissions: ['SEND_MESSAGES', 'ATTACH_FILES'], + args: [ + { + id: 'link', + type: 'url', + unordered: true + }, + { + id: 'compression', + type: 'boolean', + default: false, + unordered: true + } + ], + description: { + content: 'Compress your videos and lower their quality!', + usage: '[link to video] [lighter compression (True or false)]', + examples: [''] + } + }); + } + + async exec(message, args) { + let vid; + if (args.vid) + vid = args.vid.href; + else + vid = await attachment(message); + + let output = `${os.tmpdir()}/tmp${message.id}.mp4`; + let output2 = `${os.tmpdir()}/Shittyfied${message.id}.mp4`; + + let compression = '30k'; + let audioCompression = '60k'; + + if (args.compression) { + compression = '50k'; + audioCompression = '100k'; + } + + let loadingmsg = await message.channel.send('Processing '); + + downloader(vid, null, `${os.tmpdir()}/${message.id}.mp4`) + .on('error', async err => { + loadingmsg.delete(); + return message.channel.send(err, { code: true }); + }) + .on('end', async downloadOutput => { + let ffmpegCommand = ffmpeg(downloadOutput); + + ffmpegCommand.videoFilters('scale=iw/4:ih/4'); + ffmpegCommand.output(output); + ffmpegCommand.run(); + ffmpegCommand.on('error', (err, stdout, stderr) => { + loadingmsg.delete(); + console.error(`${err}\n${stdout}\n${stderr}`); + return message.channel.send('Uh oh, an error has occurred!' + err); + }); + ffmpegCommand.on('end', () => { + let ffmpegCommand = ffmpeg(downloadOutput); + + ffmpegCommand.videoFilters('scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2'); + ffmpegCommand.videoCodec('libx264'); + ffmpegCommand.fps(15); + ffmpegCommand.videoBitrate(compression); + ffmpegCommand.audioBitrate(audioCompression); + ffmpegCommand.output(output2); + ffmpegCommand.run(); + ffmpegCommand.on('error', (err, stdout, stderr) => { + loadingmsg.delete(); + console.error(`${err}\n${stdout}\n${stderr}`); + return message.channel.send('Uh oh, an error has occurred!' + err); + }); + ffmpegCommand.on('end', () => { + loadingmsg.delete(); + message.delete(); + return message.channel.send({files: [output2]}) + .catch(err => { + console.error(err); + return message.channel.send(`${err.name}: ${err.message} ${err.message === 'Request entity too large' ? 'The file size is too big' : ''}`); + }); + }); + }); + }); + } +} + +module.exports = vidshittyfierCommand; \ No newline at end of file diff --git a/commands/utility/gif2vid.js b/commands/utility/gif2vid.js index 69787b36..12a234e1 100644 --- a/commands/utility/gif2vid.js +++ b/commands/utility/gif2vid.js @@ -1,9 +1,8 @@ const { Command } = require('discord-akairo'); -const fs = require('fs'); const os = require('os'); -const util = require('util'); -const exec = util.promisify(require('child_process').exec); -const superagent = require('superagent'); +const ffmpeg = require('fluent-ffmpeg'); +const attachment = require('../../utils/attachment'); +const downloader = require('../../utils/download'); class gif2vidCommand extends Command { constructor() { @@ -26,49 +25,45 @@ class gif2vidCommand extends Command { } async exec(message, args) { + let vid; + if (args.vid) + vid = args.vid.href; + else + vid = await attachment(message); - let Attachment = (message.attachments).array(); - let vid = args.vid; - // Get attachment link - if (Attachment[0] && !args.vid) { - vid = Attachment[0].url; - } + if (!vid.endsWith('gif')) return message.channel.send('Please use a gif.'); let loadingmsg = await message.channel.send('Processing '); - if (!vid) { - loadingmsg.delete(); - return message.channel.send('I need a gif to do that!'); - } else if (vid) { - - const { body: buffer } = await superagent.get(vid).catch(() => { + downloader(vid, null, `${os.tmpdir()}/${message.id}g2v`) + .on('error', async err => { loadingmsg.delete(); - return message.channel.send('An error as occured, please try again'); + return message.channel.send(err, { code: true }); + }) + .on('end', async output => { + let ffmpegCommand = ffmpeg(output); + + ffmpegCommand.videoFilters('scale=trunc(iw/2)*2:trunc(ih/2)*2'); + ffmpegCommand.fps(args.fps ? args.fps : 15); + ffmpegCommand.output(`${os.tmpdir()}/${message.id}g2v.mp4`); + ffmpegCommand.outputOptions('-pix_fmt yuv420p'); + ffmpegCommand.run(); + ffmpegCommand.on('error', (err, stdout, stderr) => { + loadingmsg.delete(); + console.error(`${err}\n${stdout}\n${stderr}`); + return message.channel.send('Uh oh, an error has occurred!' + err); + }); + ffmpegCommand.on('end', () => { + loadingmsg.delete(); + message.delete(); + return message.channel.send({files: [`${os.tmpdir()}/${message.id}g2v.mp4`]}) + .catch(err => { + console.error(err); + return message.channel.send(`${err.name}: ${err.message} ${err.message === 'Request entity too large' ? 'The file size is too big' : ''}`); + }); + }); }); - - - fs.writeFile(`${os.tmpdir()}/${message.id}g2v.gif`, buffer, () => { - exec(`ffmpeg -i ${os.tmpdir()}/${message.id}g2v.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" ${os.tmpdir()}/${message.id}g2v.mp4 -hide_banner`) - .then(() => { - loadingmsg.delete(); - message.delete(); - return message.channel.send({files: [`${os.tmpdir()}/${message.id}g2v.mp4`]}) - .catch(err => { - console.error(err); - loadingmsg.delete(); - return message.channel.send('Could not send the file! Perhaps the file is too big?'); - }); - }) - .catch(err => { - console.error(err); - loadingmsg.delete(); - return message.channel.send('There was an error during conversion!'); - }); - }); - - } - } } diff --git a/commands/utility/vid2gif.js b/commands/utility/vid2gif.js index 04fb36e4..02840ef3 100644 --- a/commands/utility/vid2gif.js +++ b/commands/utility/vid2gif.js @@ -1,9 +1,8 @@ const { Command } = require('discord-akairo'); -const fs = require('fs'); const os = require('os'); -const util = require('util'); -const exec = util.promisify(require('child_process').exec); -const superagent = require('superagent'); +const ffmpeg = require('fluent-ffmpeg'); +const attachment = require('../../utils/attachment'); +const downloader = require('../../utils/download'); class vid2gifCommand extends Command { constructor() { @@ -35,60 +34,42 @@ class vid2gifCommand extends Command { } async exec(message, args) { - let Attachment = (message.attachments).array(); - let vid = args.vid; - // Get attachment link - if (Attachment[0] && !args.vid) { - vid = Attachment[0].url; - } + let vid; + + if (args.vid) + vid = args.vid.href; + else + vid = await attachment(message); let loadingmsg = await message.channel.send('Processing '); - if (!vid) { - loadingmsg.delete(); - return message.channel.send('I need a video to do that!'); - } else if (vid) { - const { body: buffer } = await superagent.get(vid).catch(() => { - loadingmsg.delete(); - return message.channel.send('An error as occured, please try again'); + downloader(vid, null, `${os.tmpdir()}/${message.id}v2g`) + .on('error', async err => { + return message.channel.send(err, { code: true }); + }) + .on('end', async output => { + let ffmpegCommand = ffmpeg(output); + + if (args.scale) ffmpegCommand.videoFilters('scale=iw/2:ih/2'); + ffmpegCommand.fps(args.fps ? args.fps : 15); + ffmpegCommand.output(`${os.tmpdir()}/${message.id}v2g.gif`); + ffmpegCommand.run(); + ffmpegCommand.on('error', (err, stdout, stderr) => { + loadingmsg.delete(); + console.error(`${err}\n${stdout}\n${stderr}`); + return message.channel.send('Uh oh, an error has occurred!' + err); + }); + ffmpegCommand.on('end', () => { + loadingmsg.delete(); + message.delete(); + return message.channel.send({files: [`${os.tmpdir()}/${message.id}v2g.gif`]}) + .catch(err => { + console.error(err); + return message.channel.send(`${err.name}: ${err.message} ${err.message === 'Request entity too large' ? 'The file size is too big' : ''}`); + }); + }); }); - let options = ''; - - if (args.scale) { - options = '-vf "scale=iw/2:ih/2"'; - } - - if (args.fps) { - options += ` -r ${args.fps}`; - } else { - options += ' -r 15'; - } - - - - - fs.writeFile(`${os.tmpdir()}/${message.id}v2g`, buffer, () => { - exec(`ffmpeg -i ${os.tmpdir()}/${message.id}v2g ${options} ${os.tmpdir()}/${message.id}v2g.gif -hide_banner`) - .then(() => { - loadingmsg.delete(); - message.delete(); - return message.channel.send({files: [`${os.tmpdir()}/${message.id}v2g.gif`]}) - .catch(err => { - console.error(err); - loadingmsg.delete(); - return message.channel.send('Could not send the file! Perhaps the file is too big?'); - }); - }) - .catch(err => { - console.error(err); - loadingmsg.delete(); - return message.channel.send('There was an error during conversion! maybe try with another file type?'); - }); - }); - - } - } }