2019-09-30 21:07:51 +02:00
|
|
|
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');
|
|
|
|
|
2019-10-12 15:18:24 +02:00
|
|
|
class vid2gifCommand extends Command {
|
2019-09-30 21:07:51 +02:00
|
|
|
constructor() {
|
2019-10-12 15:18:24 +02:00
|
|
|
super('vid2gif', {
|
2019-10-12 15:17:20 +02:00
|
|
|
aliases: ['vid2gif', 'v2g', 'vg'],
|
2019-09-30 21:07:51 +02:00
|
|
|
category: 'utility',
|
2019-11-09 12:04:01 +01:00
|
|
|
clientPermissions: ['SEND_MESSAGES', 'ATTACH_FILES'],
|
2019-09-30 21:07:51 +02:00
|
|
|
args: [
|
|
|
|
{
|
|
|
|
id: 'vid',
|
|
|
|
type: 'string'
|
2019-09-30 21:27:08 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'fps',
|
|
|
|
type: 'integer'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'scale',
|
|
|
|
match: 'flag',
|
|
|
|
flag: '--scale'
|
2019-09-30 21:07:51 +02:00
|
|
|
}
|
|
|
|
],
|
|
|
|
description: {
|
2019-09-30 21:27:08 +02:00
|
|
|
content: 'Transform video into gif. --scale to scale the gif by half,',
|
|
|
|
usage: '[link to video] [Number of fps]',
|
2019-09-30 21:07:51 +02:00
|
|
|
examples: ['']
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async exec(message, args) {
|
2020-07-11 01:33:27 +02:00
|
|
|
let Attachment = (message.attachments).array();
|
2019-09-30 21:07:51 +02:00
|
|
|
let vid = args.vid;
|
2020-07-11 01:33:27 +02:00
|
|
|
// Get attachment link
|
|
|
|
if (Attachment[0] && !args.vid) {
|
|
|
|
vid = Attachment[0].url;
|
|
|
|
}
|
2019-09-30 21:07:51 +02:00
|
|
|
|
2019-10-12 15:17:20 +02:00
|
|
|
let loadingmsg = await message.channel.send('Processing <a:loadingmin:527579785212329984>');
|
|
|
|
|
2019-09-30 21:07:51 +02:00
|
|
|
if (!vid) {
|
2019-10-12 15:17:20 +02:00
|
|
|
loadingmsg.delete();
|
2019-09-30 21:07:51 +02:00
|
|
|
return message.channel.send('I need a video to do that!');
|
|
|
|
} else if (vid) {
|
|
|
|
|
|
|
|
const { body: buffer } = await superagent.get(vid).catch(() => {
|
2019-10-12 15:17:20 +02:00
|
|
|
loadingmsg.delete();
|
2019-09-30 21:07:51 +02:00
|
|
|
return message.channel.send('An error as occured, please try again');
|
|
|
|
});
|
2019-09-30 21:27:08 +02:00
|
|
|
let options = '';
|
|
|
|
|
|
|
|
if (args.scale) {
|
|
|
|
options = '-vf "scale=iw/2:ih/2"';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.fps) {
|
|
|
|
options += ` -r ${args.fps}`;
|
2019-10-12 12:16:28 +02:00
|
|
|
} else {
|
|
|
|
options += ' -r 15';
|
2019-09-30 21:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-09-30 21:07:51 +02:00
|
|
|
|
|
|
|
fs.writeFile(`${os.tmpdir()}/${message.id}v2g`, buffer, () => {
|
2019-10-12 12:16:28 +02:00
|
|
|
exec(`ffmpeg -i ${os.tmpdir()}/${message.id}v2g ${options} ${os.tmpdir()}/${message.id}v2g.gif -hide_banner`)
|
2019-09-30 21:07:51 +02:00
|
|
|
.then(() => {
|
2019-10-12 15:17:20 +02:00
|
|
|
loadingmsg.delete();
|
|
|
|
message.delete();
|
2019-09-30 21:07:51 +02:00
|
|
|
return message.channel.send({files: [`${os.tmpdir()}/${message.id}v2g.gif`]})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err);
|
2019-10-12 15:17:20 +02:00
|
|
|
loadingmsg.delete();
|
2019-09-30 21:07:51 +02:00
|
|
|
return message.channel.send('Could not send the file! Perhaps the file is too big?');
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err);
|
2019-10-12 15:17:20 +02:00
|
|
|
loadingmsg.delete();
|
2019-09-30 21:07:51 +02:00
|
|
|
return message.channel.send('There was an error during conversion! maybe try with another file type?');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-12 15:18:24 +02:00
|
|
|
module.exports = vid2gifCommand;
|