1
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Haha-Yes/commands/fun/midify.js

125 lines
3.9 KiB
JavaScript

5 years ago
const { Command } = require('discord-akairo');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const youtubedl = require('youtube-dl');
const os = require('os');
4 years ago
const filetype = require('file-type');
const fs = require('fs');
5 years ago
class midifyCommand extends Command {
constructor() {
super('midify', {
aliases: ['midify', 'wav2midi', 'w2m', 'mp32midi', 'm2m', 'sound2midi', 's2m'],
category: 'fun',
clientPermissions: ['SEND_MESSAGES', 'ATTACH_FILES'],
args: [
{
id: 'link',
type: 'string',
4 years ago
},
{
id: 'raw',
type: 'flag',
flag: '--raw'
5 years ago
}
],
description: {
4 years ago
content: 'Transform the audio into midi --raw to get the .mid file',
5 years ago
usage: '[link to video/music/whatever you want to be midi]',
examples: ['https://www.youtube.com/watch?v=kXYiU_JCYtU']
}
});
}
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;
}
4 years ago
let input = `${os.tmpdir()}/${message.id}`;
5 years ago
let input2 = `${os.tmpdir()}/${message.id}.wav`;
let output = `${os.tmpdir()}/${message.id}.mid`;
let output2 = `${os.tmpdir()}/${message.id}.mp3`;
let loadingmsg = await message.channel.send('Processing (this can take some time) <a:loadingmin:527579785212329984>');
5 years ago
if (url) {
4 years ago
return youtubedl.exec(url, ['-o', input], {}, async function(err) {
5 years ago
if (err) {
console.error(err);
loadingmsg.delete();
return message.channel.send('An error has occured, I can\'t download from the link you provided.');
4 years ago
}
let ext = 'mp4';
if (fs.existsSync(`${os.tmpdir()}/${input}`)) {
ext = await filetype.fromFile(`${os.tmpdir()}/${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
5 years ago
}
4 years ago
input = `${os.tmpdir()}/${message.id}.${ext}`;
// Convert to wav
exec(`ffmpeg -i ${input} ${input2}`)
.then(() => {
midify();
})
.catch(err => {
console.error(err);
return message.channel.send('Oh no! an error has occured during the conversion, are you sure it is a valid file?');
});
5 years ago
});
} else {
return message.channel.send('You need a valid video link!');
}
function midify() {
// wav to midi
exec(`waon -i ${input2} -o ${output}`)
.then(() => {
4 years ago
if (args.raw) {
loadingmsg.delete();
return message.channel.send({files: [output]})
.catch(err => {
console.error(err);
loadingmsg.delete();
return message.channel.send('On no! an error just occured! perhaps the file is too big?');
});
}
5 years ago
// midi to mp3 so we can listen from discord
exec(`timidity ${output} -Ow -o - | ffmpeg -i - -acodec libmp3lame -ab 64k ${output2}`)
.then(() => {
loadingmsg.delete();
return message.channel.send({files: [output2]})
.catch(err => {
console.error(err);
loadingmsg.delete();
return message.channel.send('On no! an error just occured! perhaps the file is too big?');
});
})
.catch(err => {
console.error(err);
return message.channel.send('Oh no! an error has occured during the conversion, are you sure it is a valid file?');
});
})
.catch(err => {
console.error(err);
return message.channel.send('Oh no! an error has occured during the conversion, are you sure it is a valid file?');
});
}
}
}
module.exports = midifyCommand;