Page 1 of 1
How to check whether a subtitle was found and downloaded
Posted: 19 Feb 2015, 11:49
by Zignature
Hi folks,
I'm a newbie when it comes to writing batch files and using command line filebot.
I'm trying to create a batch file that is supposed to check for embedded subtitles using mkvmerge. If embedded subtitles are found, it checks whether there are Dutch subtitles.
If there are no Dutch subtitles or no subtitles at all, Filebot searches for Dutch subtitles (strict) and mkvmerge merges them into the MKV file.
My question is this: How do I verify that a subtitle is downloaded? Are there any return or error codes I could check?
Any help would be greatly appreciated.
PS
I'm creating a Windows 8.1 batch file (.bat)
Re: How to check whether a subtitle was found and downloaded
Posted: 19 Feb 2015, 12:03
by Zignature
This the basic logic:
Code: Select all
find subtitle in mkv
if not found {
search for subtitle
if not found {
do nothing
} else {
merge subtitle into mkv
}
} else {
is Dutch subtitle?
if not Dutch {
search subtitle
if not found {
do nothing
} else {
merge subtitle into mkv
}
} else {
do nothing
}
}
Re: How to check whether a subtitle was found and downloaded
Posted: 19 Feb 2015, 14:28
by rednoah
You'll probably want to implement that logic in Groovy. FileBot provides helpful functions like getMediaInfo() and getSubtitles() so it shouldn't be lot of code once you've figured it out.
If you plan on doing things via CMD or bash you'll just have to play with the filebot command and see what works. You can always parse some info from the output.
Re: How to check whether a subtitle was found and downloaded
Posted: 19 Feb 2015, 17:36
by Zignature
I can of course check for the existence of the subtitle file using IF EXIST but I think it would be cleaner and faster to use exit codes or anything else within the code.
Parsing the output sounds feasible.
So how do I refer to the output? Can I write the output to a variable? Like I said, I'm a newbie when it comes to batch (cmd) writing
Re: How to check whether a subtitle was found and downloaded
Posted: 19 Feb 2015, 17:50
by Zignature
Thanks a bunch!
I already figured out how to read the output into a variable e.g.
Code: Select all
filebot [parameters] > fboutput.txt
SET /p fboutput = < fboutput.txt
Re: How to check whether a subtitle was found and downloaded
Posted: 19 Feb 2015, 18:42
by Zignature
I just figured out how to parse the output. In case anyone else wonders, here's the code:
Code: Select all
FOR /f %%a IN ('filebot -get-subtitles "C:\video.mkv" --lang nl --output srt --encoding utf8 ^| FIND /c /i "Writing" ') DO (
IF [%%a]==[0] (
ECHO Subtitle not found
) ELSE (
ECHO Found subtitle!
)
)
Re: How to check whether a subtitle was found and downloaded
Posted: 21 Feb 2015, 17:18
by Zignature
And this is the result:
sub-mkv.bat for finding and merging Dutch subtitles for MKV videos
Code: Select all
@ECHO OFF &SETLOCAL disableDelayedExpansion
SET /p rootfolder=Enter The Root folder path:%=%
ECHO the root folder path is: %rootfolder%
ECHO.
ECHO Enumerating all MKVs under %rootfolder%
REM loop through files
FOR /r %rootfolder% %%a in (*.mkv) DO (
FOR /f %%b in ('mkvmerge -i "%%a" ^| FIND /c /i "subtitles" ') DO (
IF [%%b]==[0] (
CALL :SEARCHSUB "%%a" "%%~dpna.merging%%~xa" "%%~dpna%%~xa" "%%~na" "%%~dpna.nld.srt"
) ELSE (
FOR /f %%c in ('mkvmerge -i "%%a" ^| FINDSTR /i /r /c:"subtitles.*dut" ') DO (
IF %ERRORLEVEL GTR 0 (
CALL :SEARCHSUB "%%a" "%%~dpna.merging%%~xa" "%%~dpna%%~xa" "%%~na" "%%~dpna.nld.srt"
)
)
)
)
)
:SEARCHSUB
FOR /f %%d IN ('filebot -get-subtitles %1 --lang nl --output srt --encoding utf8 ^| FIND /c /i "writing" ') DO (
IF [%%d] NEQ [0] (
CALL :SUBMERGE %2 %3 %4 %5
)
)
GOTO:EOF
:SUBMERGE
mkvmerge -o %1 -S %2 --language 0:dut %4
DEL /f %2
DEL /f %4
filebot -script fn:replace --def "e=.merging.mkv" "r=.mkv" %1
GOTO:EOF
I'll be diving into Groovy to create a Groovy script

I've never coded in Java before, let alone Groovy, so it'll be a challenge
Re: How to check whether a subtitle was found and downloaded
Posted: 22 Feb 2015, 20:56
by Zignature
Well I'm able to get all MKV files but that's it. I'll stick to my batch file

I really do not have a clue on how to execute another program (mkvmerge.exe in this case) from a groovy script.
This stuff is too difficult for me to grasp
Code: Select all
// filebot -script findmkv.groovy <folder>
args.getFiles().findAll { getMediaInfo(file:it, format:'''{ext}''')=='mkv' }.each {
println getMediaInfo(file:it, format:'''{fn}.{ext}''')
}
Re: How to check whether a subtitle was found and downloaded
Posted: 23 Feb 2015, 05:45
by rednoah
If you can make anything complicate at all with Windows CMD I'm sure Groovy won't be much of a challenge!
Execute with Groovy:
http://groovy.codehaus.org/Executing+Ex ... rom+Groovy
Also FileBot provides a
execute(String...) function that you can use as well. On Windows it'll call things via cmd /c to make sure things work as one would expect.
Re: How to check whether a subtitle was found and downloaded
Posted: 23 Feb 2015, 09:15
by Zignature
Awesome! thanks for the info

Re: How to check whether a subtitle was found and downloaded
Posted: 23 Feb 2015, 09:18
by Zignature
Hmmm it seems codehaus.org is down

Re: How to check whether a subtitle was found and downloaded
Posted: 23 Feb 2015, 09:27
by Zignature
They're back

Re: How to check whether a subtitle was found and downloaded
Posted: 24 Feb 2015, 02:18
by Zignature
I think I finally did it. It took me the better part of the day and I suspect, being a noob at this, the code could be cleaner.
sub-mkv.groovy:
Code: Select all
// filebot -script "path\to\sub-mkv.groovy" "<path\to\folder>"
args.getFiles().findAll { getMediaInfo(file:it, format:'{ext}')=='mkv' }.each {
def filepath = it.toString()
def mkvinfo = ["mkvmerge", "-I", it]
def proc = mkvinfo.execute()
proc.waitFor()
def exitcode = "${proc.exitValue()}".toInteger()
def procOutput = "${proc.in.text}"
def regex = ~/subtitles.*language:dut/
if (exitcode == 0) {
if (regex.matcher(procOutput).getCount() == 0) {
if ( getSubtitles(file:it, lang:'nl', output:'srt', encoding:'utf8', strict:'true').size() > 0) {
def srtfile = new File ( filepath.replaceFirst('.mkv', '.nld.srt') )
def outputfile = new File ( filepath.replaceFirst('.mkv', '.merging.mkv') )
def mkvtitle = filepath.tokenize('\\').last().replaceFirst('.mkv', '')
def mkvmerge = ["mkvmerge", "-o", outputfile, "-S", it, "--language", "0:dut", "--title", mkvtitle, srtfile]
proc = mkvmerge.execute()
proc.waitFor()
exitcode = "${proc.exitValue()}".toInteger()
if (exitcode == 0) {
it.delete()
srtfile.delete()
new File(outputfile.toString()).renameTo(new File(it.toString()))
}
}
}
}
}
Re: How to check whether a subtitle was found and downloaded
Posted: 25 Feb 2015, 02:16
by Zignature