Page 1 of 1

[help] get a subtitle for a specific video file with script

Posted: 26 May 2016, 12:14
by Zignature
I'm trying to create a post-processing script for Sonarr.
Everytime a video is downloaded the scipt is supposed to download the correct Dutch subtitle.

I already got something going, but it seems the script is checking all files in the directory instead of the specific video file.

I also can't seem to figure out how to pass a language code to the script....

The batch file called by Sonarr:

Code: Select all

@ECHO OFF
@setlocal EnableDelayedExpansion

:: [Sonarr AutoSubber] by Zignature

:: NOTES:
:: Filebot.exe needs to be in your system's PATH (Environment Variable) setting!!!
:: Sonarr needs to run as an application. NOT as a Windows service!

:: Invoke Filebot to search for subtitle by hash (strict mode).
START "Searching subtitle..." /WAIT filebot -script "E:\Scripts\sonarr-autosubber.groovy" "%sonarr_episodefile_path%"
The Groovy script that handles everything:

Code: Select all

// filebot -script "sonarr-autosubber.groovy" "<path\to\file]>"
args.getFiles().findAll {
	getSubtitles(file:it, lang:'nl', output:'srt', encoding:'utf8', strict:'true')
}.each {
	// Define the full path
	def filePath = it.toString();
	// Define the starting point of the full filename
	def fileNameIdx = filePath.lastIndexOf('\\') + 1
	// Define the full path to the current directory
	def parentPath = filePath[0..fileNameIdx-1]
	// The filename without the dot and the extension
	def fileName = filePath.substring(fileNameIdx,filePath.length()-4)
	// Define the source subtitle file
	def src_srt = new File(parentPath.toString() + fileName.toString() + ".nld.srt")
	// Define the destination subtitle file
	def dst_srt = new File(parentPath.toString() + fileName.toString() + ".nl.srt")
	// Check whether a subtitle was downloaded and then rename.
	if(src_srt.exists()) {
		// Check whether the subtitle was already renamed.
		if(!dst_srt.exists()) {
			// Create new subtitle file with "nl" language code
			dst_srt.write("")
			// Copy the contents from the source subtitle file
			dst_srt << src_srt.text
			// Delete source subtitle file
			src_srt.delete()
		}
	}
}
Any tips regarding code clean-up would also be greatly appreciated :)

Re: [help] get a subtitle for a specific video file with scr

Posted: 26 May 2016, 13:30
by rednoah
Just use the suball script. I don't see a reason why you'd need or want to write your own script.

You could pass the language via --lang option, but you're overriding it in your script anyway.

Also, if you want to process a single file instead of a folder, you need to pass in a single file and not a folder. ;)

Re: [help] get a subtitle for a specific video file with scr

Posted: 26 May 2016, 14:44
by Zignature
There's a big demand for an automated subtitle downloader in the Sonarr community, so I thought I'd give it a go.
And I just like it that a subtitle will be downloaded immediately when Sonarr imports an episode :)

Somehow my Plex server doesn't seem to handle subtitle files with "nld" as the Dutch language code, so I have to rename the language code to "nl".

Until now I ran a batch file (invoking Filebot) every now and then, to do this stuff.


Anyway, thanks for the info. I'll see if it works out for me.

Re: [help] get a subtitle for a specific video file with scr

Posted: 26 May 2016, 17:13
by rednoah
This would be my solution:
https://gist.github.com/rednoah/3e12742 ... 372fa05a24

You can pass in your own files and --lang options. Downloaded subtitles will be post-renamed with ISO 639-2/B language codes.


For non-Dutch users you probably just wanna use the suball script though.


PS: Tell Plex to support ISO 639-3 already. It's the standard of language code standards. ISO 639-2/B is for librarians. :lol:

Re: [help] get a subtitle for a specific video file with scr

Posted: 26 May 2016, 18:38
by Zignature
Awesome, thanks!

Plex only seems to have problems with "nld" for Dutch. "nl" works fine, I haven't tried "dut" yet.

Re: [help] get a subtitle for a specific video file with scr

Posted: 27 May 2016, 11:58
by Zignature
Did some quick research. Plex only supports ISO 639-1 and ISO 639-2B language codes... :geek:

I posted a Feature Request: http://forums.plex.tv/discussion/219591 ... -3-support :twisted:

Re: [help] get a subtitle for a specific video file with scr

Posted: 27 May 2016, 12:06
by Zignature
By the way, is it possible to search multiple languages in one go? For instance if you feed a list of language codes like "nl,en,fr"?
Using eachWithIndex perhaps?

Re: [help] get a subtitle for a specific video file with scr

Posted: 27 May 2016, 15:06
by rednoah
You can make "multi-language support" that work like this:
https://gist.github.com/rednoah/3e12742 ... 372fa05a24

Re: [help] get a subtitle for a specific video file with scr

Posted: 27 May 2016, 16:41
by Zignature
Awesome!

Man I really need to do a course in Groovy... You make it look so easy and I really don't have a clue about this stuff :D

Re: [help] get a subtitle for a specific video file with scr

Posted: 27 May 2016, 17:56
by Zignature
A well-deserved donation is on it's way!

Re: [help] get a subtitle for a specific video file with scr

Posted: 27 May 2016, 18:09
by Zignature

Re: [help] get a subtitle for a specific video file with scr

Posted: 28 May 2016, 16:30
by Zignature
Since Sonarr sends environment variables, I simplified the setup for Sonarr skipping the batch file.

It now sends the following arguments straight to Filebot.exe:

Code: Select all

-script "E:\Scripts\sonarr-subber.groovy" --lang nl,en
sonarr-subber.groovy:

Code: Select all

// select input language(s)
def languages = _args.lang.split(/\W/) as List

// select input video
def env = System.getenv()
def episodeFile = env['sonarr_episodefile_path'] as String

// fetch missing subtitles
def subtitleFiles = languages.findResults{
	getMissingSubtitles(lang: it, file: episodeFile, output: 'srt', encoding: 'UTF-8')
}.flatten()

// rename subtitles
rename(map: subtitleFiles.collectEntries{
	[it, getMediaInfo(it, '{fn[0..-5]}.{lang.ISO3B}.{ext}')]
})
This also makes it cross-platform :)

Re: [help] get a subtitle for a specific video file with scr

Posted: 28 May 2016, 16:43
by Zignature
does the --conflict parameter do anything for getMissingSubtitles()?

In case Sonarr downloads an upgrade for an episode, a new subtitle needs to be downloaded and then a conflict may occur.
Or should I use getSubtitles() instead for that?

Re: [help] get a subtitle for a specific video file with scr

Posted: 28 May 2016, 17:15
by Zignature
What if I added a parameter eg. --mode, with 'd' meaning download and 'u' meaning upgrade.
Then the full argument would read:

Code: Select all

-script "E:\Scripts\sonarr-subber.groovy" --lang nl,en --mode d
Would this be feasible?

Code: Select all

// select mode, download or upgrade
def mode = _args.mode as String

// select input language(s)
def languages = _args.lang.split(/\W/) as List

// select input video
def env = System.getenv()
def episodeFile = env['sonarr_episodefile_path'] as String

// fetch (missing) subtitles
def subtitleFiles = languages.findResults{
	switch(mode) {
		case 'd' // new download
			getMissingSubtitles(lang: it, file: episodeFile, output: 'srt', encoding: 'UTF-8')
			break
		case 'u' // upgrade
			getSubtitles(lang: it, file: episodeFile, output: 'srt', encoding: 'UTF-8')
			break
	}
}.flatten()

// rename subtitles
rename(map: subtitleFiles.collectEntries{
	[it, getMediaInfo(it, '{fn[0..-5]}.{lang.ISO3B}.{ext}')]
})

Re: [help] get a subtitle for a specific video file with scr

Posted: 28 May 2016, 17:20
by rednoah
Instead of reusing the standard CLI options you can just pass in your own --def name=value variables.

Reusing --mode is probably ok though because that option is only used in the GUI so reusing it for something else shouldn't cause any issues.

Re: [help] get a subtitle for a specific video file with scr

Posted: 28 May 2016, 17:25
by Zignature
Ok. Actually I could give the parameter any name. The word mode just sprung to mind :)

But would that script work as I modified it?

If I use --def mode=d method, how would I refer to it in the script? Same way eg. _args.mode?

Re: [help] get a subtitle for a specific video file with scr

Posted: 28 May 2016, 17:29
by rednoah
No. It's just there. You pass in mode and then you just use mode. :D

Re: [help] get a subtitle for a specific video file with scr

Posted: 28 May 2016, 17:30
by Zignature
:lol:

Re: [help] get a subtitle for a specific video file with scr

Posted: 28 May 2016, 17:52
by Zignature
Just made good on the donation I promised ;)