@ -1005,10 +1005,18 @@ function registerFileDB(full_file_path, type) {
return false ;
}
// add additional info
file _object [ 'uid' ] = uuid ( ) ;
file _object [ 'registered' ] = Date . now ( ) ;
path _object = path . parse ( file _object [ 'path' ] ) ;
file _object [ 'path' ] = path . format ( path _object ) ;
// remove existing video if overwriting
db . get ( ` files. ${ type } ` )
. remove ( {
path : file _object [ 'path' ]
} ) . write ( ) ;
db . get ( ` files. ${ type } ` )
. push ( file _object )
. write ( ) ;
@ -1022,6 +1030,7 @@ function generateFileObject(id, type) {
}
const ext = ( type === 'audio' ) ? '.mp3' : '.mp4'
const file _path = getTrueFileName ( jsonobj [ '_filename' ] , type ) ; // path.join(type === 'audio' ? audioFolderPath : videoFolderPath, id + ext);
// console.
var stats = fs . statSync ( path . join ( _ _dirname , file _path ) ) ;
var title = jsonobj . title ;
@ -1117,31 +1126,15 @@ async function downloadFileByURL_exec(url, type, options, sessionID = null) {
timestamp _start : Date . now ( )
} ;
const download = downloads [ session ] [ download _uid ] ;
updateDownloads ( ) ;
await new Promise ( resolve => {
youtubedl . exec ( url , [ ... downloadConfig , '--dump-json' ] , { } , function ( err , output ) {
if ( output ) {
let json = JSON . parse ( output [ 0 ] ) ;
const output _no _ext = removeFileExtension ( json [ '_filename' ] ) ;
download [ 'expected_path' ] = output _no _ext + ext ;
download [ 'expected_json_path' ] = output _no _ext + '.info.json' ;
resolve ( true ) ;
} else if ( err ) {
logger . error ( err . stderr ) ;
} else {
logger . error ( ` Video info retrieval failed. Download progress will be unavailable for URL ${ url } ` ) ;
}
} ) ;
} ) ;
updateDownloads ( ) ;
youtubedl . exec ( url , downloadConfig , { } , function ( err , output ) {
download [ 'downloading' ] = false ;
download [ 'timestamp_end' ] = Date . now ( ) ;
var file _uid = null ;
let new _date = Date . now ( ) ;
let difference = ( new _date - date ) / 1000 ;
logger . debug ( ` Video download delay: ${ difference } seconds. ` ) ;
logger . debug ( ` ${ is _audio ? 'Audio' : 'Video' } download delay: ${ difference } seconds. ` ) ;
if ( err ) {
logger . error ( err . stderr ) ;
@ -1195,7 +1188,7 @@ async function downloadFileByURL_exec(url, type, options, sessionID = null) {
}
// registers file in DB
file _uid = registerFileDB ( full _file _path . substring ( fileFolderPath . length , full _file _path . length ) , 'video' ) ;
file _uid = registerFileDB ( full _file _path . substring ( fileFolderPath . length , full _file _path . length ) , type ) ;
if ( file _name ) file _names . push ( file _name ) ;
}
@ -1262,14 +1255,13 @@ async function downloadFileByURL_normal(url, type, options, sessionID = null) {
video . on ( 'info' , function ( info ) {
video _info = info ;
file _size = video _info . size ;
console . log ( 'Download started' )
fs . writeJSONSync ( removeFileExtension ( video _info . _filename ) + '.info.json' , video _info ) ;
video . pipe ( fs . createWriteStream ( video _info . _filename , { flags : 'w' } ) )
} ) ;
// Will be called if download was already completed and there is nothing more to download.
video . on ( 'complete' , function complete ( info ) {
'use strict'
console. log ( 'filename: ' + info . _filename + ' already downloaded.' )
logger. info ( 'file ' + info . _filename + ' already downloaded.' )
} )
let download _pos = 0 ;
@ -1283,8 +1275,6 @@ async function downloadFileByURL_normal(url, type, options, sessionID = null) {
} ) ;
video . on ( 'end' , function ( ) {
console . log ( 'finished downloading!' )
let new _date = Date . now ( ) ;
let difference = ( new _date - date ) / 1000 ;
logger . debug ( ` Video download delay: ${ difference } seconds. ` ) ;
@ -1292,8 +1282,12 @@ async function downloadFileByURL_normal(url, type, options, sessionID = null) {
download [ 'complete' ] = true ;
updateDownloads ( ) ;
// Does ID3 tagging if audio
// audio-only cleanup
if ( type === 'audio' ) {
// filename fix
video _info [ '_filename' ] = removeFileExtension ( video _info [ '_filename' ] ) + '.mp3' ;
// ID3 tagging
let tags = {
title : video _info [ 'title' ] ,
artist : video _info [ 'artist' ] ? video _info [ 'artist' ] : video _info [ 'uploader' ]
@ -1377,10 +1371,11 @@ async function generateArgs(url, type, options) {
if ( customOutput ) {
downloadConfig = [ '-o' , fileFolderPath + customOutput + "" , qualityPath , '--write-info-json' , '--print-json' ] ;
} else {
downloadConfig = [ '-o' , fileFolderPath + videopath + ( is _audio ? '. mp3 ' : '.mp4' ) , qualityPath , '--write-info-json' , '--print-json' ] ;
downloadConfig = [ '-o' , fileFolderPath + videopath + ( is _audio ? '. %(ext)s ' : '.mp4' ) , qualityPath , '--write-info-json' , '--print-json' ] ;
}
if ( is _audio ) {
downloadConfig . push ( '-x' ) ;
downloadConfig . push ( '--audio-format' , 'mp3' ) ;
}
@ -1695,7 +1690,7 @@ app.post('/api/tomp3', async function(req, res) {
}
const is _playlist = url . includes ( 'playlist' ) ;
if ( is _playlist )
if ( true || is _playlist )
result _obj = await downloadFileByURL _exec ( url , 'audio' , options , req . query . sessionID ) ;
else
result _obj = await downloadFileByURL _normal ( url , 'audio' , options , req . query . sessionID ) ;