Fix midify

This commit is contained in:
loicbersier 2020-03-02 15:22:27 +01:00
parent 66c0367265
commit b0b663b8d0

View file

@ -5,6 +5,8 @@ const youtubedl = require('youtube-dl');
const os = require('os'); const os = require('os');
const filetype = require('file-type'); const filetype = require('file-type');
const fs = require('fs'); const fs = require('fs');
const ffmpeg = require('fluent-ffmpeg');
class midifyCommand extends Command { class midifyCommand extends Command {
constructor() { constructor() {
@ -57,27 +59,29 @@ class midifyCommand extends Command {
} }
let ext = 'mp4'; let ext = 'mp4';
if (fs.existsSync(`${os.tmpdir()}/${input}`)) { if (fs.existsSync(input)) {
ext = await filetype.fromFile(`${os.tmpdir()}/${input}`); ext = await filetype.fromFile(input);
ext = ext.ext; // This look stupid but hey, it work 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 ) 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}`); fs.renameSync(input, `${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 } else if (fs.existsSync(`${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}.mkv`, `${input}.mp4`); // Discord play mkv just fine but it need to end with mp4
} }
input = `${os.tmpdir()}/${message.id}.${ext}`; input = `${os.tmpdir()}/${message.id}.${ext}`;
// Convert to wav // Convert to wav
exec(`ffmpeg -i ${input} ${input2}`) ffmpeg()
.then(() => { .input(input)
.output(input2)
.on('end', () => {
midify(); midify();
}) })
.catch(err => { .on('error', (err, stdout, stderr) => {
console.error(err); 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?'); return message.channel.send('Oh no! an error has occured during the conversion, are you sure it is a valid file?');
}); })
.run();
}); });
} else { } else {
return message.channel.send('You need a valid video link!'); return message.channel.send('You need a valid video link!');