From b0b663b8d095d39591cefd7615fe02fc001a7728 Mon Sep 17 00:00:00 2001 From: loicbersier Date: Mon, 2 Mar 2020 15:22:27 +0100 Subject: [PATCH] Fix midify --- commands/fun/midify.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/commands/fun/midify.js b/commands/fun/midify.js index 13520745..8e0d8d06 100644 --- a/commands/fun/midify.js +++ b/commands/fun/midify.js @@ -5,6 +5,8 @@ const youtubedl = require('youtube-dl'); const os = require('os'); const filetype = require('file-type'); const fs = require('fs'); +const ffmpeg = require('fluent-ffmpeg'); + class midifyCommand extends Command { constructor() { @@ -57,27 +59,29 @@ class midifyCommand extends Command { } let ext = 'mp4'; - if (fs.existsSync(`${os.tmpdir()}/${input}`)) { - ext = await filetype.fromFile(`${os.tmpdir()}/${input}`); + 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(`${os.tmpdir()}/${input}`, `${os.tmpdir()}/${input}.${ext}`); - } else if (fs.existsSync(`${os.tmpdir()}/${input}.mkv`)) { // If it can't find the video assume it got merged and end with mkv - fs.renameSync(`${os.tmpdir()}/${input}.mkv`, `${os.tmpdir()}/${input}.mp4`); // Discord play mkv just fine but it need to end with mp4 + 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 - exec(`ffmpeg -i ${input} ${input2}`) - .then(() => { + ffmpeg() + .input(input) + .output(input2) + .on('end', () => { midify(); }) - .catch(err => { - console.error(err); + .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!');