Page 1 of 2

[SCRIPT] Automated multilingual subtitle grabber for Sonarr

Posted: 28 May 2016, 18:57
by Zignature
Custom script for Sonarr for automated multilingual subtitle downloading.
This script uses Filebot to search for missing subtitles by hash in the given language(s) and
saves the file with ISO 639-2/B language code(s).
This is handy for Plex users for instance, since Plex doesn't support ISO 639-3 codes.

If multiple languages are required add them as a comma-separated list (without spaces!) eg. nl,en,fr,de


Prerequisites:
  1. Sonarr - TV Series Manager (https://sonarr.tv/)
  2. Filebot (Of course :D)
  3. Filebot added to your system's PATH (Environment Variable) setting
  4. On Windows systems run Sonarr as an application! NOT as a Windows Service! Sonarr as a Windows service doesn't execute custom scripts.
  5. An Opensubtitles.org account to login to Opensubtitles in Filebot

Installation/Setup:
  1. Install Sonarr
  2. Install Filebot.
  3. Copy sonarr-subber.groovy to your preferred location on your harddrive
  4. In Sonarr navigate to Sonarr -> Settings -> Connect -> [+] -> Custom Script
  5. In the Pop-up enter the following:
    • Name: Enter a descriptive name to your liking eg. Sonarr Subtitle Downloader
    • On Grab: No
    • On Download: Yes
    • On Upgrade: Yes
    • On Rename: No
    • Filter Series Tags: Use at your own discretion
    • Path: Full path to Filebot. (On Windows systems: C:\Program Files\FileBot\filebot.exe)
    • Arguments: -script "FULL-PATH-TO\sonarr-subber.groovy" --lang [2-letter ISO 639-1 language code(s)]
  6. Click on the Test button.
  7. If the test succeeded click on the Save button.
This script also runs after manual imports.


Sonarr Custom Script Arguments Examples:
In these examples it's assumed you copied sonarr-subber.groovy to C:\MyScripts\ on a Windows system.

Search for Dutch subtitles:

Code: Select all

-script "C:\MyScripts\sonarr-subber.groovy" --lang nl
Search for Dutch, English and French subtitles:

Code: Select all

-script "C:\MyScripts\sonarr-subber.groovy" --lang nl,en,fr

The Script:

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 subtitles
def subtitleFiles = languages.findResults{
	getSubtitles(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}')]
})
Million thanks go out to rednoah for his awesome support!

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 28 May 2016, 19:01
by Zignature
Since I don't have a Linux or Mac system, perhaps someone could tell me the path settings for Filebot on those systems.
Path settings for XPenology would also be greatly appreciated :)

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 03 Jun 2016, 16:37
by edge33
hello, good work with this script, I have some questions:

1)When the script is run, how does it find what download triggered it?
2) I see you use a environment variable to fetch subtitles, How does it work?

I am running Sonarr And filebot on Centos 6

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 03 Jun 2016, 17:12
by Zignature
Sonarr creates environment variables on the fly.

https://github.com/Sonarr/Sonarr/wiki/C ... ng-Scripts

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 03 Jun 2016, 17:42
by Zignature
in the version on my pc I also pass a variable to determine which ISO 639 version to use, and then use a switch to set it.

Code: Select all

--def iso639=<1|2|3>

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 03 Jun 2016, 17:49
by edge33
thank you for your support, everything is working fine under Centos 6

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 03 Jun 2016, 17:52
by Zignature
You're welcome :)

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 06 Jun 2016, 02:41
by vfx
Hello,
Tried this with sonarr, it downloaded subtitle all right. But it appended _eng.srt to the subtitle which doesn't exactly match the video file name because of extra "eng" in the sub srt file (e.g. Mr. Robot - S01E05 - eps1.4_3xpl0its.wmv.eng.srt)

so I changed the following to:
[it, getMediaInfo(it, '{fn[0..-5]}.{lang.ISO3B}.{ext}')]

to

[it, getMediaInfo(it, '{fn[0..-5]}.{ext}')]


It no longer appended "eng" to the srt file. Sorry. I had no idea all the syntax does. But that was what I figured it would do.

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 06 Jun 2016, 05:46
by Zignature
The script has to append the language code to the filename, because it's possible to search for multiple languages. Imagine you want both English and Dutch subtitles...

Does the ".eng" in the filename cause any problems for you?

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 06 Jun 2016, 18:38
by vfx
No. but some stupid video player won't auto display the subtitle if the sub srt file not the same as the video file. I only need eng sub so it would be ok. Anyhow, opensubtitles doesn't have a lot of TV sub as subscence or addic7ed. So it's a hit or miss. It's not a 100% automation per se.

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 06 Jun 2016, 20:28
by edge33
Hi, it may well happen that when sonarr finds a new episode, the subtitle is not already available, so, How would I force the script to retry after a while?

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 06 Jun 2016, 21:27
by vfx
the script only executed on download or upgrade to a better release. You'll have to manually download it with the app unless you want to redownload the eps again.

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 07 Jun 2016, 04:31
by vfx
Just found this script. Worked pretty well to download TV subs with addic7ed and some other providers (well as in it can download subs ok, but navigation leaves something to desire more). Good Luck.

https://github.com/h3llrais3r/Auto-Subliminal

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 07 Jun 2016, 19:05
by Zignature
vfx wrote:Hello,
Tried this with sonarr, it downloaded subtitle all right. But it appended _eng.srt to the subtitle which doesn't exactly match the video file name because of extra "eng" in the sub srt file (e.g. Mr. Robot - S01E05 - eps1.4_3xpl0its.wmv.eng.srt)

so I changed the following to:
[it, getMediaInfo(it, '{fn[0..-5]}.{lang.ISO3B}.{ext}')]

to

[it, getMediaInfo(it, '{fn[0..-5]}.{ext}')]


It no longer appended "eng" to the srt file. Sorry. I had no idea all the syntax does. But that was what I figured it would do.
I just read your post and was about to suggest that :D

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 07 Jun 2016, 19:08
by Zignature
edge33 wrote:Hi, it may well happen that when sonarr finds a new episode, the subtitle is not already available, so, How would I force the script to retry after a while?
I wouldn't know at the moment :(
I have Sonarr setup to download episodes only if they're 7 days old. This way chances are better to find subtitles

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 07 Jun 2016, 19:13
by Zignature
You could always setup a scheduled task on your machine to run Filebot to check for missing subtitles

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 07 Jun 2016, 19:21
by rednoah
The suball script has options for only download subs for files that are older than a certain amount of time. Setting minAgeDays to 3-4 days should be good enough.

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 10 Jun 2016, 00:03
by vfx
rednoah wrote:The suball script has options for only download subs for files that are older than a certain amount of time. Setting minAgeDays to 3-4 days should be good enough.
Some good info here: viewtopic.php?f=4&t=5#p8871

I tried the other auto-subliminal script, but kinda unreliable. I want to use fileboot. Got like a boat-load of questions:

1) Does filebot do recursive folder looking? Can it download video file located in a sub-folder, sub-folder (e.g. TV Shows --> Silicon Valley --> Season 01)
2) "you MUST SET --def maxAgeDays to 30 days or less and call it no more than once per day". What exactly does maxAgeDays do? How do you prevent to not call it more than once a day?
3) I want to download ENG sub only. What is the option? Not clear on --lang zho. What is zho?
4) More coming... gtg

Thanks in advance.

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 10 Jun 2016, 00:21
by Zignature
vfx wrote:
rednoah wrote:The suball script has options for only download subs for files that are older than a certain amount of time. Setting minAgeDays to 3-4 days should be good enough.
Some good info here: viewtopic.php?f=4&t=5#p8871

I tried the other auto-subliminal script, but kinda unreliable. I want to use fileboot. Got like a boat-load of questions:

1) Does filebot do recursive folder looking? Can it download video file located in a sub-folder, sub-folder (e.g. TV Shows --> Silicon Valley --> Season 01)
2) "you MUST SET --def maxAgeDays to 30 days or less and call it no more than once per day". What exactly does maxAgeDays do? How do you prevent to not call it more than once a day?
3) I want to download ENG sub only. What is the option? Not clear on --lang zho. What is zho?
4) More coming... gtg

Thanks in advance.
1. Yes
2. Rednoah better answers this one :)
3. Add

Code: Select all

--lang en

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 10 Jun 2016, 04:54
by rednoah
2a.
--def maxAgeDays=7 set a max-age for files that will be processed, older files will be ignored
2b.
You use the cron or scheduler and tell it to run your script once per day. You don't need Sonarr for that at all.

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 11 Jun 2016, 05:37
by vfx
So I used Task Scheduler to set up suball. It ran and downloaded all subtitles for me. Pretty slick. Thanks a millions.

By the way, it has that .eng in subtitle file (e.g. superslickfilebot.eng.srt). Is there a way to not append .eng in the subtitle? I used the command line below:

filebot -script fn:suball /path/to/media -non-strict --def maxAgeDays=30

Schedule not to run more than 1day.

By the way, does the script keep a log that I can check to see what subtitles have been downloaded instead of going into each folder and check?

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 11 Jun 2016, 05:42
by rednoah
1.
Try setting --format MATCH_VIDEO and see if the script respects that option.

2.
Use the --log option to specify a log file.

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 11 Jun 2016, 05:46
by vfx
rednoah wrote:1.
Try setting --format MATCH_VIDEO and see if the script respects that option.

2.
Use the --log option to specify a log file.
Thanks again.


How do you skip the subtitles that have already been downloaded?

--log /path/log.txt?

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 11 Jun 2016, 05:53
by rednoah
It'll do that automatically. Make sure to check the logs to confirm that it's working correctly.

Re: [SCRIPT] Automated multilingual subtitle grabber for Son

Posted: 11 Jun 2016, 14:28
by vfx
Thanks again.

Does Filebot portable support running script? I tried to use it but couldn't run the suball script to download subs.