Haha-Yes/commands/utility/vid2gif.js

76 lines
2.1 KiB
JavaScript
Raw Normal View History

2019-09-30 21:07:51 +02:00
const { Command } = require('discord-akairo');
const os = require('os');
2020-07-16 09:20:24 +02:00
const ffmpeg = require('fluent-ffmpeg');
const attachment = require('../../utils/attachment');
const downloader = require('../../utils/download');
2019-09-30 21:07:51 +02:00
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', {
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: 'url'
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-16 09:20:24 +02:00
let vid;
2019-09-30 21:07:51 +02:00
2020-07-16 09:20:24 +02:00
if (args.vid)
vid = args.vid.href;
else
vid = await attachment(message);
2019-09-30 21:27:08 +02:00
2020-07-16 09:20:24 +02:00
let loadingmsg = await message.channel.send('Processing <a:loadingmin:527579785212329984>');
2019-09-30 21:27:08 +02:00
2020-07-16 09:20:24 +02:00
downloader(vid, null, `${os.tmpdir()}/${message.id}v2g`)
.on('error', async err => {
return message.channel.send(err, { code: true });
})
.on('end', async output => {
let ffmpegCommand = ffmpeg(output);
2019-09-30 21:07:51 +02:00
2020-07-16 09:20:24 +02:00
if (args.scale) ffmpegCommand.videoFilters('scale=iw/2:ih/2');
ffmpegCommand.fps(args.fps ? args.fps : 15);
ffmpegCommand.output(`${os.tmpdir()}/${message.id}v2g.gif`);
ffmpegCommand.run();
ffmpegCommand.on('error', (err, stdout, stderr) => {
loadingmsg.delete();
console.error(`${err}\n${stdout}\n${stderr}`);
return message.channel.send('Uh oh, an error has occurred!' + err);
});
ffmpegCommand.on('end', () => {
loadingmsg.delete();
message.delete();
return message.channel.send(`Converted by ${message.author}`, {files: [`${os.tmpdir()}/${message.id}v2g.gif`]})
2020-07-16 09:20:24 +02:00
.catch(err => {
console.error(err);
return message.channel.send(`${err.name}: ${err.message} ${err.message === 'Request entity too large' ? 'The file size is too big' : ''}`);
});
});
2019-09-30 21:07:51 +02:00
});
}
}
2019-10-12 15:18:24 +02:00
module.exports = vid2gifCommand;