2019-10-12 12:16:19 +02:00
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' ) ;
2019-10-12 16:49:07 +02:00
class vidshittifierCommand extends Command {
2019-10-12 12:16:19 +02:00
constructor ( ) {
2019-10-12 16:49:07 +02:00
super ( 'vidshittifier' , {
aliases : [ 'vidshittifier' , 'vs' , 'shittifier' , 'vid2shit' , 'v2s' ] ,
2019-10-12 12:16:19 +02:00
category : 'fun' ,
2019-11-09 12:04:01 +01:00
clientPermissions : [ 'SEND_MESSAGES' , 'ATTACH_FILES' ] ,
2019-10-12 12:16:19 +02:00
args : [
{
id : 'link' ,
type : 'string' ,
2019-10-12 12:48:36 +02:00
} ,
{
2019-10-12 19:39:36 +02:00
id : 'compression' ,
2019-10-12 18:04:31 +02:00
type : 'integer'
2019-10-12 12:16:19 +02:00
}
] ,
description : {
2019-10-12 12:48:36 +02:00
content : 'Make your vid shit quality.' ,
usage : '[link to video] [compression ( 1, 2 or 3)]' ,
2019-10-12 12:16:19 +02:00
examples : [ '' ]
}
} ) ;
}
async exec ( message , args ) {
2019-10-12 19:26:35 +02:00
let Attachment = ( message . attachments ) . array ( ) ;
let url = args . link ;
// Get attachment link
if ( Attachment [ 0 ] && ! args . link ) {
url = Attachment [ 0 ] . url ;
}
2019-10-12 18:01:10 +02:00
let input = ` ${ os . tmpdir ( ) } / ${ message . id } .mp4 ` ;
2019-11-09 12:04:01 +01:00
let input2 = ` ${ os . tmpdir ( ) } /tmp ${ message . id } .mp4 ` ;
2019-10-13 00:19:08 +02:00
let output = ` ${ os . tmpdir ( ) } /Shittified ${ message . id } .mp4 ` ;
2019-10-12 19:39:36 +02:00
2019-10-12 12:48:36 +02:00
let compression ;
2019-10-12 19:39:36 +02:00
let audioCompression ;
if ( args . compression == 1 ) {
2019-10-12 18:09:31 +02:00
compression = '50k' ;
2019-10-12 19:39:36 +02:00
audioCompression = '100k' ;
2019-11-09 12:04:01 +01:00
} else {
2019-10-12 18:09:31 +02:00
compression = '30k' ;
2019-10-12 19:39:36 +02:00
audioCompression = '60k' ;
2019-10-12 14:50:52 +02:00
}
2019-11-09 12:04:01 +01:00
2019-10-12 19:39:36 +02:00
let option = ` -b:v ${ compression } -b:a ${ audioCompression } ` ;
2019-10-12 13:11:21 +02:00
2019-10-12 15:10:11 +02:00
let loadingmsg = await message . channel . send ( 'Processing <a:loadingmin:527579785212329984>' ) ;
2019-10-12 19:26:35 +02:00
if ( url ) {
2019-10-28 14:41:19 +01:00
return youtubedl . exec ( url , [ '--format=mp4' , '-o' , input ] , { } , function ( err ) {
2019-10-12 14:50:52 +02:00
if ( err ) {
console . error ( err ) ;
2019-10-12 15:10:11 +02:00
loadingmsg . delete ( ) ;
2019-10-12 14:50:52 +02:00
return message . channel . send ( 'An error has occured, I can\'t download from the link you provided.' ) ;
2019-10-12 18:30:17 +02:00
} else {
shittifie ( ) ;
2019-10-12 14:50:52 +02:00
}
2019-10-12 12:16:19 +02:00
} ) ;
} else {
return message . channel . send ( 'You need a valid video link!' ) ;
}
2019-10-12 14:50:52 +02:00
2019-10-12 16:49:07 +02:00
function shittifie ( ) {
2019-11-09 12:04:01 +01:00
// reduce video resolution
exec ( ` ffmpeg -i ${ input } -vf "scale=iw/4:ih/4" ${ input2 } ` )
2019-10-12 14:50:52 +02:00
. then ( ( ) => {
2019-11-09 12:04:01 +01:00
// upscale video and change bitrate
exec ( ` ffmpeg -i ${ input2 } ${ option } -vf "scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2" -vcodec libx264 -r 15 -f mp4 ${ output } ` )
. then ( ( ) => {
loadingmsg . delete ( ) ;
message . 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?' ) ;
} ) ;
} )
2019-10-12 14:50:52 +02:00
. catch ( err => {
console . error ( err ) ;
2019-10-12 15:10:11 +02:00
loadingmsg . delete ( ) ;
2019-11-09 12:04:01 +01:00
return message . channel . send ( 'On no! an error just occured! Im gonna be honest with you, i don\'t know what caused it yet! but worry not! my owner will look into it soon!' ) ;
2019-10-12 14:50:52 +02:00
} ) ;
} ) ;
}
2019-10-12 12:16:19 +02:00
}
}
2019-10-12 16:49:07 +02:00
module . exports = vidshittifierCommand ;