[SCRIPT] Download subtitles and merge/remux into MKV container

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
Zignature
Power User
Posts: 48
Joined: 19 Feb 2015, 11:33

[SCRIPT] Download subtitles and merge/remux into MKV container

Post by Zignature »

Batch download Dutch subtitles and merge/remux them into a Matroska video container (MKV)

License:
This script is free for everyone to use or edit. Just don't sell it :P

Requirements:
  • Windows operating system (for now)
  • sub-merge-mkv-groovy.bat (included in zip file)
  • sub-merge-mkv.groovy (included in zip file, fully commented)
  • MKVToolnix (Jean Claude Van Damme at https://www.bunkus.org/videotools/mkvtoolnix/)
  • Both Filebot and MKVToolnix need to be added to the system environment PATH variable
Installation: Usage:
  • Either run sub-merge-mkv-groovy.bat
  • Or run in CMD/CLI

    Code: Select all

    filebot -script /path/to/sub-merge-mkv.groovy /path/to/files
What does it do:
  • When sub-merge-mkv-groovy.bat is executed it asks for the folder to be scanned
  • The script then recursively scans the given folder for MKV video files
  • For every MKV video file it finds:
    1. it scans the file for existing Dutch subtitles using MKVToolnix (MKVmerge.exe)
    2. if MKVToolnix detects Dutch subtitles, it moves on to the next MKV video file
    3. if MKVToolnix doesn't detect Dutch subtitles, Filebot searches for subtitles (filetype SubRip/SRT, encoding UTF8, strict mode)
    4. if Filebot doesn't find Dutch subtitles, it moves on to the next MKV video file
    5. if Filebot finds Dutch subtitles, MKVToolnix merges/remuxes them into the MKV video file and deletes the original SRT file
You need it for another language?
This script can easily be adapted to suit other languages.
You need to edit the following lines in the fully commented version of sub-merge-mkv.groovy:

On line 24 change:

Code: Select all

def regex = ~/subtitles.*language:dut/
e.g. for English:

Code: Select all

def regex = ~/subtitles.*language:eng/
On line 31 change:

Code: Select all

lang:'nl'
e.g. for English:

Code: Select all

lang:'en'
line 35 change:

Code: Select all

'.nld.srt'
e.g. for English:

Code: Select all

'.eng.srt'
On line 44 change:

Code: Select all

"0:dut"
e.g. for English:

Code: Select all

"0:eng"

Disclaimer:
  • This script has only been tested an run on Windows (8.1). I have no idea whether this will work on other platforms. I'm sure with a little tweaking it will.
  • Use this script at your own peril. I'll take no responsibility for any loss of data or any damage. I suggest running it a couple of times on a test folder first.
sub-merge-mkv-groovy.bat

Code: Select all

@ECHO OFF
SET /p mediafolder=Enter The folder path to scan:%=%
ECHO the folder path is: %mediafolder%
ECHO.
filebot -script "sub-merge-mkv.groovy" %mediafolder%
PAUSE
sub-merge-mkv.groovy

Code: Select all

// filebot -script "path\to\sub-mkv.groovy" <folder>

subsskipped = 0
subsfound = 0
subsmerged = 0
subsnotfound = 0
subsnotmerged = 0

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 ) {
				subsfound++
				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 ) {
					subsmerged++
					it.delete()
					srtfile.delete()
				    new File ( outputfile.toString() ).renameTo ( new File ( it.toString() ) )
				} else {
					subsnotmerged++
				}
			} else {
				subsnotfound++
			}
		} else {
			subsskipped++
		}
	}
}

println "\nResults: skipped: " + subsskipped + ", not found: " + subsnotfound +  ", found: " + subsfound + ", merged: " + subsmerged + ", not merged: " + subsnotmerged + "\n"
nytram
Posts: 30
Joined: 12 Feb 2014, 04:34

Re: [SCRIPT] Download subtitles and merge/remux into MKV con

Post by nytram »

Hi

WOW thats really awesome make me wish I used windows [not really :- )] though after a quick look I think it will work in a unix env too.

Playing now!!

great work.

Martyn
garyleecn
Donor
Posts: 114
Joined: 19 Nov 2014, 03:48

Re: [SCRIPT] Download subtitles and merge/remux into MKV con

Post by garyleecn »

wish it could work on mac too.

maybe someone could merge it into filebot itself
Post Reply