2023-08-21 11:46:48 -05:00
const ytdl = require ( 'ytdl-core' ) ,
fs = require ( 'fs' ) ,
path = require ( 'path' ) ,
express = require ( 'express' ) ,
2023-10-18 20:04:42 -05:00
ffmpeg = require ( 'ffmpeg' ) ,
2023-08-24 08:48:41 -05:00
bodyParser = require ( 'body-parser' )
2023-05-05 11:15:22 -05:00
const PORT = process . env . PORT || 8080
const app = express ( )
2023-10-20 18:03:54 -05:00
if ( fs . existsSync ( path . join ( _ _dirname , 'cached' ) ) ) {
fs . unlinkSync ( path . join ( _ _dirname , 'cached' ) )
}
fs . mkdirSync ( path . join ( _ _dirname , 'cached' ) )
const cacheDuration = 15 || process . env . CACHEDUR
2023-05-05 11:15:22 -05:00
2023-08-24 11:00:33 -05:00
process . on ( 'uncaughtException' , ( err , origin ) => {
fs . writeSync (
process . stderr . fd ,
` Caught exception: ${ err } \n ` +
` Exception origin: ${ origin } ` ,
) ;
} ) ;
2023-08-23 11:35:10 -05:00
2023-08-24 11:00:33 -05:00
app . use ( bodyParser . urlencoded ( { extended : false } ) )
2023-08-23 11:35:10 -05:00
2023-10-20 18:03:54 -05:00
app . get ( "/getLink" , async ( req , res ) => {
2023-05-08 12:52:41 -05:00
const filename = req . query . filename
const url = req . query . url
2023-05-10 12:36:26 -05:00
const format = req . query . format
2023-05-05 11:15:22 -05:00
const quality = req . query . quality
2023-05-10 12:36:26 -05:00
2023-08-28 08:33:16 -05:00
if ( ! ytdl . validateURL ( url ) ) {
2023-10-20 18:03:54 -05:00
res . header ( "Content-Type" , "text/html" )
res . write ( ` <link rel="stylesheet" href="/style.css"> ` )
res . write ( ` <p>Invalid URL! Check the url for any errors! <br> ${ url } </p> ` )
2023-08-24 11:00:33 -05:00
return
}
2023-10-20 18:03:54 -05:00
res . setHeader ( "Content-Type" , "text/html" ) ;
2023-08-23 11:35:10 -05:00
2023-10-20 18:03:54 -05:00
res . write ( ` <link rel="stylesheet" href="/style.css"> ` )
// String(Math.floor(Math.random() * 100000)) +
var ytpath = path . join ( _ _dirname , 'cached/' + ytdl . getVideoID ( url ) + '.mp4' )
2023-10-18 20:04:42 -05:00
var vidinfo = await ytdl . getBasicInfo ( url )
2023-10-20 18:03:54 -05:00
var lastp = 0
var ytvid = ytdl ( url , { 'quality' : quality , 'format' : 'mp4' } )
. on ( "progress" , ( data , ctotal , etotal ) => {
var integer = 1
var percent = ( Math . ceil ( ctotal / etotal * 100 ) )
if ( percent % integer == 0 && lastp != percent ) {
res . write ( ` <p id="percent ${ percent } ">Downloading... ${ percent } %</p> ` )
res . write ( `
< style >
# percent$ { lastp } {
display : none ;
2023-10-18 20:04:42 -05:00
}
2023-10-20 18:03:54 -05:00
< / s t y l e >
` )
lastp = percent
}
} )
. pipe ( fs . createWriteStream ( ytpath ) )
. on ( "close" , ( ) => {
res . write ( "<p>Downloaded!</p>" )
var proc = new ffmpeg ( ytpath )
// res.write(`<p>Converting... (May take a while, <a href="https://github.com/Violets-puragtory/NoJS-YTConverter"> doesn't support progress updates yet</a>)</p>`)
var endpath = ( 'cached/' + ytdl . getVideoID ( url ) + String ( Math . floor ( Math . random ( ) * 100000 ) ) + "Converted." + format )
res . write ( ` <p id="convert ${ 0 } ">Converting... 0% (Potentially Innacurate, Experimental!)</p> ` )
proc . then ( function ( video ) {
video
. setVideoDuration ( vidinfo . videoDetails . lengthSeconds )
. setVideoFormat ( format )
. save ( endpath , ( error , file ) => {
res . write ( "<p>Converted!</p>" )
res . write ( ` <p><a href="/download?path= ${ path . basename ( file ) } &filename= ${ filename } . ${ format } " target="_blank">Link to video download!</a> (Link is deleted after 15 minutes!)</p> ` )
setTimeout ( ( ) => {
if ( fs . existsSync ( ytpath ) ) {
fs . unlinkSync ( ytpath )
}
if ( fs . existsSync ( file ) ) {
fs . unlinkSync ( file )
}
} , 60 * 1000 )
} )
2023-10-18 20:04:42 -05:00
} )
2023-10-20 18:03:54 -05:00
var integer = 1
var lastc = 0
var percent = 0
async function update ( ) {
if ( fs . existsSync ( endpath ) ) {
var ffile = await fs . readFileSync ( endpath )
var yt = await fs . readFileSync ( ytpath )
percent = ( Math . round ( ffile . length / yt . length * ( 100 / integer ) ) ) * integer
if ( percent % integer == 0 && percent != lastc ) {
res . write ( ` <p id="convert ${ percent } ">Converting... ${ Math . min ( percent , 100 ) } % (Potentially Innacurate, Experimental!)</p> ` )
res . write ( `
< style >
# convert$ { lastc } {
display : none ;
}
< / s t y l e >
` )
lastc = percent
}
}
if ( percent < 100 ) {
setTimeout ( ( ) => {
update ( )
} , 500 ) ;
}
}
update ( )
2023-10-18 20:04:42 -05:00
} )
2023-10-20 18:03:54 -05:00
} ) ;
2023-08-21 11:46:48 -05:00
2023-10-20 18:03:54 -05:00
app . get ( '/download' , ( req , res ) => {
var file = req . query . path
var filename = req . query . filename
var dest = path . join ( _ _dirname , 'cached/' , file )
2023-05-05 11:15:22 -05:00
2023-10-20 18:03:54 -05:00
if ( fs . existsSync ( dest ) ) {
res . header ( 'Content-Disposition' , ` attachment; filename=" ${ filename } " ` ) ;
fs . createReadStream ( dest ) . pipe ( res )
} else {
res . header ( "Content-Type" , "text/html" )
res . write ( ` <link rel="stylesheet" href="/style.css"> ` )
res . write ( ` <p>Uh oh! It seems that video couldn't be found... Maybe the URL expired? Or, the link was invalid. <br>If you believe this was a mistake, please put a issue on <a href="https://github.com/Violets-puragtory/NoJS-YTConverter/">Github</a></p> ` , ( ) => {
res . end ( )
} )
}
} )
2023-05-05 11:15:22 -05:00
app . use ( express . static ( path . join ( _ _dirname , 'static' ) ) )
2023-05-10 12:36:26 -05:00
app . listen ( PORT , function ( ) {
2023-05-05 11:15:22 -05:00
console . log ( "Hosted on port " + PORT )
} )