@ -1,33 +1,25 @@
const fs = require ( 'fs' ) ;
const execSync = require ( 'child_process' ) . execSync ;
function details ( ) {
return {
id : "Tdarr_Plugin_e5c3_CnT_Add_Subtitles" ,
Stage : "Pre-processing" ,
Name : "Add subtitles to MKV files" ,
Type : "Video" ,
Operation : "Remux" ,
Description : ` Add subtitles. READ THIS!! You must run "npm install iso-639-2" in the folder "/home/Tdarr/Documents" for this plugin to work. This is a plugin that will check for subtitles, they should be named according to the ISO 639-2 language code. ` ,
Version : "1. 00 ",
Description : ` This plugin will check for subtitles, they should be named according to the ISO 639-2 language code.\n A subtitle could look like this: eng.srt \n If there are subtitles found they will be added with FFMPEG, if there are no subs of that language found. \n On first run node module iso-639-2 will be installed in the documents folder. \n Created by @control#0405 ` ,
Version : "1. 3 ",
Link : "https://github.com/HaveAGitGat/Tdarr_Plugins/blob/master/Community/Tdarr_Plugin_e5c3_CnT_Add_Subtitles.js" ,
Inputs : [
{
name : 'install_packages' ,
tooltip : ` Please change this to "yes", it allows the plugin to install the required nodemodule. (iso-639-2) `
} ,
]
}
}
function plugin ( file ) {
//Additional packages used here so details function above will still work if packages aren't installed
const iso6392 = require ( '/home/Tdarr/Documents/node_modules/iso-639-2' ) ;
var i = 0 ; //int for counting lang[position]
var sub = 0 ; //becomes first subtitle stream
var lang = iso6392 ; //languages to check against
var path = file . meta . Directory ; //path of media folder
var exist = 0 ; //if the language exists should be added this becomes 1
var new _subs = 0 //count the new subs
var added _subs = 0 ; //counts the amount of subs that have been mapped
var preset _import = '' ;
var preset _meta = '' ;
function plugin ( file , librarySettings , inputs , otherArguments ) {
//default response
var response = {
processFile : false ,
@ -36,50 +28,86 @@ function plugin(file) {
handBrakeMode : false ,
FFmpegMode : false ,
reQueueAfter : false ,
infoLog : ` Testing for subtitles...\n Path: ${ path } \n ` ,
infoLog : ` Searching new subtitles... \n ` ,
}
//find first subtitle stream
while ( file . ffProbeData . streams [ sub ] . codec _type . toLowerCase ( ) != "subtitle" ) {
sub ++
if ( inputs . install _packages == "yes" ) {
if ( ! fs . existsSync ( ` ${ otherArguments . homePath } /Tdarr/node_modules/iso-639-2 ` ) ) {
execSync ( ` cd ${ otherArguments . homePath } /Tdarr \n npm install iso-639-2 ` ) ;
}
} else {
response . infoLog = ` Please take a look at the input options \n A extra nodemodule is required. `
return response ;
}
response . infoLog += ` The first subtitle stream is ${ sub } \n `
if ( fs . existsSync ( ` /home/Tdarr/Documents/Tdarr/node_modules/iso-639-2 ` ) ) {
var iso6392 = require ( '/home/Tdarr/Documents/Tdarr/node_modules/iso-639-2' ) ;
} else {
response . infoLog += ` Nodemodule iso-639-2 isn't installed! \n Try Again `
return response ;
}
var i = 0 ; //int for counting lang[position]
var found _subtitle _stream = 0 ;
var sub = 0 ; //becomes first subtitle stream
var lang = iso6392 ; //languages to check against
var path = file . meta . Directory ; //path of media folder
var exist = 0 ; //if the language exists should be added this becomes 1
var new _subs = 0 //count the new subs
var added _subs = 0 ; //counts the amount of subs that have been mapped
var preset _import = '' ;
var preset _meta = '' ;
//find first subtitle stream
while ( found _subtitle _stream == 0 && sub < file . ffProbeData . streams . length ) {
if ( file . ffProbeData . streams [ sub ] . codec _type . toLowerCase ( ) == "subtitle" ) {
found _subtitle _stream = 1 ;
} else {
sub ++ ;
}
}
response . infoLog += ` Path: ${ path } \n ` ;
for ( i = 0 ; i < lang . length ; i ++ ) {
//check if srt exists in folder
//check if srt file exists in folder
if ( fs . existsSync ( ` ${ path } / ${ lang [ i ] . iso6392B } .srt ` ) ) {
response . infoLog += ` Found subtitle ${ lang [ i ] . name } \n `
response . infoLog += ` Found subtitle ${ lang [ i ] . name } .srt \n` ;
//check if language already exists
for ( sub _stream = sub ; sub _stream < file . ffProbeData . streams . length ; sub _stream ++ ) {
response . infoLog += ` does ${ lang [ i ] . name } exist in stream ${ sub _stream } ? \n `
if ( file . ffProbeData . streams [ sub _stream ] . tags . language . toLowerCase ( ) == lang [ i ] . iso6392B ) {
response . infoLog += ` YES \n `
exist = 1 ;
} else {
response . infoLog += ` NO \n `
if ( found _subtitle _stream == 1 ) {
//check if language already exists
for ( sub _stream = sub ; sub _stream < file . ffProbeData . streams . length ; sub _stream ++ ) {
//response.infoLog += `does ${lang[i].name} exist in stream ${sub_stream}?\n`
if ( file . ffProbeData . streams [ sub _stream ] . tags . language ) {
if ( file . ffProbeData . streams [ sub _stream ] . tags . language . toLowerCase ( ) == lang [ i ] . iso6392B ) {
//response.infoLog += `YES\n`
exist = 1 ;
response . infoLog += ` Language already exists in stream ${ sub _stream } \n It will not be added \n ` ;
}
}
}
} else {
exist = 0 ;
}
//add if it hasn't found the language
if ( exist != 1 ) {
preset _import += ` -sub_charenc "UTF-8" -f srt -i " ${ path } / ${ lang [ i ] . iso6392B } .srt" `
preset _meta += ` -metadata:s:s: ${ new _subs } language= ${ lang [ i ] . iso6392B } `
new _subs ++
preset _import += ` -sub_charenc "UTF-8" -f srt -i " ${ path } / ${ lang [ i ] . iso6392B } .srt" ` ;
preset _meta += ` -metadata:s:s: ${ new _subs } language= ${ lang [ i ] . iso6392B } ` ;
new _subs ++ ;
}
} else {
response . infoLog += ` did not find sub ${ lang [ i ] . iso6392B } .srt \n `
}
}
//else {
// response.infoLog += `did not find sub ${lang[i].iso6392B}.srt\n`
//}
exist = 0 ;
}
response . infoLog += ` ${ new _subs } new subs will be added \n `
response . preset += ` ${ preset _import } ${ preset _meta } -map 0:v -map 0:a `
//map new subs
while ( added _subs < new _subs ) {
added _subs ++
response . preset += ` -map ${ added _subs } :s `
added _subs ++ ;
response . preset += ` -map ${ added _subs } :s ` ;
}
//if new subs have been found they will be added
@ -87,12 +115,16 @@ function plugin(file) {
response . FFmpegMode = true ;
response . processFile = true ;
response . reQueueAfter = true ;
response . preset += ` -map 0:s -c copy `
if ( found _subtitle _stream == 1 ) {
response . preset += ` -map 0:s ` ;
}
response . preset += ` -c copy ` ;
response . infoLog += ` ${ new _subs } new subs will be added \n ` ;
} else {
response . infoLog += ` No new subtitle languages were found \n `
response . infoLog += ` No new subtitle languages were found \n ` ;
}
response . infoLog += ` The ffmpeg string is: ${ response . preset } \n `
//response.infoLog += `The ffmpeg string is: ${response.preset}\n`
return response
}