Haha-Yes/commands/fun/midify.js

132 lines
3.6 KiB
JavaScript
Raw Normal View History

2019-11-22 16:27:16 +01:00
const { Command } = require('discord-akairo');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const downloader = require('../../utils/download');
2019-11-22 16:27:16 +01:00
const os = require('os');
2020-03-02 15:22:27 +01:00
const ffmpeg = require('fluent-ffmpeg');
2019-11-22 16:27:16 +01:00
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',
2020-04-09 16:28:10 +02:00
match: 'rest'
2020-02-19 23:33:23 +01:00
},
{
id: 'raw',
2020-04-09 16:28:10 +02:00
match: 'flag',
flag: ['--raw']
},
{
id: 'noteblock',
match: 'flag',
flag: ['--noteblock']
},
{
id: 'voice',
match: 'flag',
flag: ['--voice']
2019-11-22 16:27:16 +01:00
}
],
description: {
2020-02-19 23:33:23 +01:00
content: 'Transform the audio into midi --raw to get the .mid file',
2019-11-22 16:27:16 +01:00
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;
}
2020-02-28 00:31:08 +01:00
let input = `${os.tmpdir()}/${message.id}`;
2019-11-22 16:27:16 +01:00
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>');
2019-11-22 16:27:16 +01:00
if (url) {
downloader(url, null, input)
2020-07-11 01:33:12 +02:00
.on('error', (err) => {
2019-11-22 16:27:16 +01:00
loadingmsg.delete();
return message.channel.send(err, { code: true });
})
2020-07-11 01:33:12 +02:00
.on('end', 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();
});
2019-11-22 16:27:16 +01:00
} else {
return message.channel.send('You need a valid video link!');
}
function midify() {
// wav to midi
exec(`waon -i ${input2} -o ${output}`)
.then(() => {
2020-02-19 23:33:23 +01:00
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?');
});
}
2020-04-09 16:28:10 +02:00
let option;
if (args.noteblock) {
option = '-c ./asset/timidity/config/noteblock.cfg';
} else if (args.voice) {
option = '-c ./asset/timidity/config/voice.cfg';
}
2019-11-22 16:27:16 +01:00
// midi to mp3 so we can listen from discord
2020-04-09 16:28:10 +02:00
exec(`timidity ${output} ${option} -Ow -o - | ffmpeg -i - -acodec libmp3lame -ab 64k ${output2}`)
2019-11-22 16:27:16 +01:00
.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;