2020-02-27 17:48:32 +01:00
const { Command } = require ( 'discord-akairo' ) ;
const ffmpeg = require ( 'fluent-ffmpeg' ) ;
const fetch = require ( 'node-fetch' ) ;
const fs = require ( 'fs' ) ;
const os = require ( 'os' ) ;
class audio2imageCommand extends Command {
constructor ( ) {
super ( 'audio2image' , {
aliases : [ 'audio2image' , 'a2i' ] ,
category : 'fun' ,
clientPermissions : [ 'SEND_MESSAGES' , 'ATTACH_FILES' ] ,
args : [
{
id : 'video_size' ,
match : 'option' ,
2020-02-27 17:52:25 +01:00
flag : '--size' ,
default : '640x480'
2020-03-11 00:54:51 +01:00
} ,
{
id : 'link' ,
type : 'string' ,
2020-02-27 17:48:32 +01:00
}
] ,
description : {
2020-06-15 17:56:51 +02:00
content : 'Transform an audio file into an image.Use --size (a number) to get a bigger image! (NOTE: bigger image might fail, so be careful!)' ,
2020-02-27 17:48:32 +01:00
usage : '[link to audio] [--size anumber]' ,
2020-02-27 22:06:50 +01:00
examples : [ 'https://cdn.discordapp.com/attachments/532987690145021982/682654351772221480/jeff.wav (optional) --size 1920x1080' ]
2020-02-27 17:48:32 +01:00
}
} ) ;
}
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 ;
}
let loadingmsg = await message . channel . send ( 'Processing <a:loadingmin:527579785212329984>' ) ;
if ( ! url ) return message . channel . send ( 'Please attach an audio file or use an url' ) ;
fetch ( url )
. then ( res => {
const dest = fs . createWriteStream ( ` ${ os . tmpdir ( ) } / ${ message . id } ` ) ;
res . body . pipe ( dest ) ;
dest . on ( 'finish' , ( ) => {
ffmpeg ( ` ${ os . tmpdir ( ) } / ${ message . id } ` ) // Convert to raw pcm
. audioBitrate ( 44100 )
. audioChannels ( 1 )
. format ( 's16le' )
. audioCodec ( 'pcm_s16le' )
. output ( ` ${ os . tmpdir ( ) } / ${ message . id } 1.sw ` )
2020-02-27 17:57:16 +01:00
. on ( 'error' , ( err , stdout , stderr ) => {
console . error ( ` ${ err } \n ${ stdout } \n ${ stderr } ` ) ;
return message . channel . send ( 'Uh oh, an error has occured!' ) ;
} )
2020-02-27 17:48:32 +01:00
. on ( 'end' , ( ) => {
ffmpeg ( )
. input ( ` ${ os . tmpdir ( ) } / ${ message . id } 1.sw ` )
. inputOption ( '-pixel_format rgb24' )
2020-02-27 17:52:25 +01:00
. inputOption ( ` -video_size ${ args . video _size } ` )
2020-02-27 17:48:32 +01:00
. inputFormat ( 'rawvideo' )
. frames ( '1' )
. output ( ` ${ os . tmpdir ( ) } /a2i ${ message . id } .png ` )
2020-02-27 17:57:16 +01:00
. on ( 'error' , ( err , stdout , stderr ) => {
console . error ( ` ${ err } \n ${ stdout } \n ${ stderr } ` ) ;
2020-06-15 17:56:51 +02:00
return message . channel . send ( 'Uh oh, an error has occured! The image size is most likely bigger than the content! Try again!' ) ;
2020-02-27 17:57:16 +01:00
} )
2020-02-27 17:48:32 +01:00
. on ( 'end' , ( ) => {
console . log ( 'finished' ) ;
loadingmsg . delete ( ) ;
2020-02-27 19:47:56 +01:00
let file = fs . statSync ( ` ${ os . tmpdir ( ) } /a2i ${ message . id } .png ` ) ;
let fileSize = ( file . size / 1000000.0 ) . toFixed ( 2 ) ;
return message . channel . send ( ` Image is ${ fileSize } MB for ${ args . video _size } resolution ` , { files : [ ` ${ os . tmpdir ( ) } /a2i ${ message . id } .png ` ] } )
2020-02-27 18:09:28 +01:00
. catch ( ( ) => {
2020-06-15 17:56:51 +02:00
return message . channel . send ( ` End result is too big to fit on discord! File is ${ fileSize } MB! Unless it's GB or even TB, in which case that is a huge image! ` ) ; } ) ;
2020-02-27 17:48:32 +01:00
} )
. run ( ) ;
} )
. run ( ) ;
} ) ;
} ) ;
}
}
module . exports = audio2imageCommand ;