2018-12-30 01:20:24 +01:00
const { Command } = require ( 'discord-akairo' ) ;
2018-11-18 17:58:08 +01:00
const fs = require ( 'fs' ) ;
const youtubedl = require ( 'youtube-dl' ) ;
2019-01-19 17:23:45 +01:00
const hbjs = require ( 'handbrake-js' ) ;
2018-11-18 17:58:08 +01:00
const { fbuser , fbpasswd } = require ( '../../config.json' ) ;
2018-12-25 19:17:07 +01:00
2018-12-30 01:20:24 +01:00
class DownloadCommand extends Command {
2019-01-02 08:09:45 +01:00
constructor ( ) {
super ( 'download' , {
aliases : [ 'download' , 'dl' ] ,
category : 'utility' ,
args : [
{
id : 'link' ,
type : 'string' ,
2019-06-23 03:41:59 +02:00
prompt : {
start : 'Send the link of wich video you want to download' ,
}
2019-03-26 01:19:43 +01:00
} ,
{
id : 'alt' ,
2019-06-23 03:41:59 +02:00
match : 'flag' ,
flag : '--alt'
2019-01-02 08:09:45 +01:00
}
] ,
clientPermissions : [ 'ATTACH_FILES' ] ,
description : {
content : 'Download videos from different website from the link you provided' ,
usage : '[link]' ,
examples : [ 'https://www.youtube.com/watch?v=6n3pFFPSlW4' ]
}
} ) ;
}
2018-11-18 17:58:08 +01:00
2019-01-02 08:09:45 +01:00
async exec ( message , args ) {
let link = args . link ;
2019-01-23 02:18:21 +01:00
let needCompress = false ;
2018-12-30 01:20:24 +01:00
2019-01-02 08:09:45 +01:00
if ( link . includes ( 'http' ) || link . includes ( 'www' ) ) {
2019-03-26 01:19:43 +01:00
if ( args . alt ) {
2019-04-08 00:34:33 +02:00
console . log ( 'alt download!' ) ;
2019-03-26 01:19:43 +01:00
fs . unlink ( './video.mp4' , ( err ) => {
2019-04-08 00:34:33 +02:00
if ( err ) ;
2019-03-26 01:19:43 +01:00
} ) ;
2019-04-08 00:34:33 +02:00
return youtubedl . exec ( args . link , [ '-o' , './video.mp4' ] , { } , function ( err , output ) {
if ( err ) throw err ;
console . log ( output . join ( '\n' ) ) ;
message . delete ( ) ;
message . channel . send ( ` Downloaded by ${ message . author . username } ` , { files : [ './video.mp4' ] } )
. catch ( ( ) => message . channel . send ( 'File too big' ) ) ;
2019-03-26 01:19:43 +01:00
} ) ;
}
2019-01-19 23:27:41 +01:00
let video = youtubedl ( link , [ ` --username= ${ fbuser } ` , ` --password= ${ fbpasswd } ` ] ) ;
2019-01-02 08:09:45 +01:00
video . pipe ( fs . createWriteStream ( './video.mp4' ) ) ;
video . on ( 'error' , function error ( err ) {
console . log ( 'error 2:' , err ) ;
message . channel . send ( 'An error has occured, I can\'t download from the link you provided.' ) ;
} ) ;
2019-03-24 18:07:18 +01:00
message . channel . send ( 'Downloading <a:loadingmin:527579785212329984>' ) . then ( msg => {
video . on ( 'end' , function ( ) {
msg . delete ( ) ;
} ) ;
} ) ;
2019-01-19 17:23:45 +01:00
video . on ( 'info' , function ( info ) {
2019-01-23 10:12:12 +01:00
let duration = info . duration ;
if ( duration ) {
duration = duration . replace ( /:/g , '' ) ;
if ( duration > 500 ) {
video . pause ( ) ;
video . unpipe ( ) ;
return message . channel . send ( 'Can\'t download video longer than 5 minutes' ) ;
}
2019-01-23 02:18:21 +01:00
}
2019-01-23 10:12:12 +01:00
2019-01-19 17:23:45 +01:00
console . log ( 'Download started' ) ;
2019-01-23 02:05:38 +01:00
console . log ( 'filename: ' + info . _filename ) ;
2019-01-19 17:23:45 +01:00
console . log ( 'size: ' + info . size ) ;
2019-01-23 02:18:21 +01:00
2019-01-19 17:23:45 +01:00
if ( info . size >= 8000000 ) {
2019-01-23 02:18:21 +01:00
needCompress = true ;
2019-01-19 17:23:45 +01:00
}
} ) ;
2019-01-02 08:09:45 +01:00
video . on ( 'end' , function ( ) {
2019-01-23 02:18:21 +01:00
if ( ! needCompress ) {
2019-01-19 17:23:45 +01:00
message . delete ( ) ;
return message . channel . send ( ` Downloaded by ${ message . author . username } ` , { files : [ './video.mp4' ] } )
. catch ( ( ) => message . channel . send ( 'File too big' ) ) ;
}
const options = {
input : 'video.mp4' ,
output : 'videoReady.mp4' ,
preset : 'General/Gmail Small 10 Minutes 288p30'
} ;
2019-01-19 17:41:55 +01:00
//Compress vid if bigger than 8MB
2019-03-24 18:07:18 +01:00
let handbrake = hbjs . spawn ( options ) ;
handbrake . on ( 'start' , function ( ) {
message . channel . send ( 'Video bigger than 8MB compressing now <a:loadingmin:527579785212329984> (This can take a long time!)\nWant it to go faster? Donate to the dev with the donate command, so i can get a better server and do it faster!' ) . then ( msg => {
handbrake . on ( 'end' , function ( ) {
msg . delete ( ) ;
} ) ;
2019-01-19 17:23:45 +01:00
} ) ;
} ) ;
2019-03-24 18:07:18 +01:00
handbrake . on ( 'error' , err => {
message . channel . send ( 'An error has occured while compressing the video' ) ;
console . error ( err ) ;
} ) ;
handbrake . on ( 'progress' , progress => {
console . log (
'Percent complete: %s, ETA: %s' ,
progress . percentComplete ,
progress . eta
) ;
} ) ;
handbrake . on ( 'end' , function ( ) {
message . delete ( ) ;
message . channel . send ( ` Downloaded by ${ message . author . username } ` , { files : [ './videoReady.mp4' ] } )
. catch ( ( ) => message . channel . send ( 'File too big' ) ) ;
} ) ;
2019-01-02 08:09:45 +01:00
} ) ;
} else {
2019-01-02 10:48:26 +01:00
2019-01-02 08:09:45 +01:00
message . channel . send ( 'You need to input a valid link' ) ;
}
}
2018-12-30 01:20:24 +01:00
}
2018-11-18 17:58:08 +01:00
2018-12-30 01:20:24 +01:00
module . exports = DownloadCommand ;