Jump to content

Script to extract subtitles from Movie


marauder

Recommended Posts

marauder

I've been running into this issue more often lately as more and more files come with lots of subtitles. My direct play on my TV causes most embedded subtitles to be transcoded. On larger movies this can be a problem maxing out my CPU. So I created a script that I put in my /usr/bin to pull out the subtitles. Figured I'd share for anyone else interested. Mine pulls out English and Russian, but can easily be changed to do any other languages. Script is set to run from the movies folder and will initiate a library scan on completion. it is currently setup wit the belief of 1 movie per folder, if you have more simply add a loop in front of the main one.

movie-subtitle-extract-emby.sh

  • Thanks 1
Link to comment
Share on other sites

  • 3 weeks later...
  • 1 month later...
Seger

Hey you

thanks a lot for sharing the script.

Can you maybe help me to get it running, would like to put it in German and English. Do I have to replace only ru with de and rus with deu? Maybe Russian with German.

Then I have a question about the execution, this runs on your machine/NAS as a script and connects "only" with Emby via the address and the API or? Can I test this first on a movie or on a test folder? Would not like to let it loose on my large collection. Maybe for your information, my NAS is a Synology.

Thank you for any help
Seger

Link to comment
Share on other sites

marauder

Hey so this script is setup so that you put it on a machine which has access to your files. For me I put this on a Debian vm with an NFS mount to my media. I put the script in my /usr/bin but you can put it anywhere in your $PATH. You’d then go to the specific movie you want to work on and run the script. 
for example, my system is setup with a folder structure like so:

main folder is called Movies. All my movie files are under this folder. Each movie is then under its own folder. 
So the movie avengers would be under

/movies/Avengers/avengers.mkv the naming is slightly different due to quality tags and release year but for this example it works. So I’d go to /movies/Avengers folder then run the script. So it would only run for that one movie. 
 

editing subtitle extraction:

so in the script this line is where you edit first. 

subtracks=$(mkvmerge -J $media |jq '.tracks[] |select(.type!="audio" and (.properties.language=="eng" or .properties.language=="en" or .properties.language=="ru" or .properties.language=="rus"))'

Some English subtitles are tagged as en or eng which is why I search for both. Same goes for Russian. To change the languages you’d replace the values with the quotes to your language. If you want to add additional you’d simply add another OR after the last “ bit before the two ) with a .properties.language==‘your language’ 

you can add as many as you want, but each one will add more time to the script running. 
 

the next block to update would be:

if [ $language == "eng" ] || [ $language == "en" ]; then
                language='English'
        fi
        if [ $language == "ru" ] || [ $language == "rus" ]; then
                language='Russian'
        fi
 

Match up the language values what you setup up top in the double quote section just have the ==. In the line below you are stating what the language is. This section is purely for esthetics as when Emby loads up the subtitles the value written here is what emby will display the subtitle name as.  If you want to add more subtitles, just copy the 3 lines from if to fi and change the language codes.
 

Hope that makes sense. typing this from my phone. The script isn’t setup to run on all your movies at once. It’s more of a one at a time thing. You could do a wrapper script on top of it to do all of them. For me I only worry about the amount of subtitles if the movie is over 20gb. That that point it can affect playback when it’s transcoding and multiple people are streaming. 

  • Thanks 2
Link to comment
Share on other sites

  • 5 months later...
t123thomas

This nice I love to give it a try, wonder if it will also work for Tvshows ? Any thoughts?

Thanks

Link to comment
Share on other sites

marauder

I have another one I use for tv shows. It’s in a different format, to this one, as I wrote it first. I’ll upload it when I’m back at my machine. 

Link to comment
Share on other sites

marauder
3 hours ago, t123thomas said:

This nice I love to give it a try, wonder if it will also work for Tvshows ? Any thoughts?

Thanks

This is the one I wrote for TVShows. I just put it as a cron and run it twice a day. I left some of my configs in there, so you can see how it's setup. You'll just need to add in your TVShow's path, The shows you want, lanaguages you want, EMBY URL and API Key. Lines 7,12,23,45

when replacing remove the {} around the value, i just put that as a marker/placeholder for you.

subtitle-strip.sh

Edited by marauder
  • Thanks 1
Link to comment
Share on other sites

marauder

Similar to the original script I posted, this one strips out unwanted subtitles in movies instead of extracts them. This one, like the movie-subtitle-extract I have in my /usr/bin and just call it from within the Movie's folder.

My folder structure is: /Media/Movies/MOVIE - Title/Movie-file.

movie-subtitle-strip.sh

  • Thanks 1
Link to comment
Share on other sites

t123thomas

Question

 I have an MKV tool on my PC same emby media server, but my movies and thanks libraries are stored on NAS.

So where do placed the script and how launch.

Thanks

Link to comment
Share on other sites

marauder

So the script is written in Bash, so it would need to be on a Linux machine or through WSL. The machine you run it would will also need mkvtoolnix and jq installed. You would then place the file in one of the system paths. Usually /usr/bin and make the file executable. Then you’d run it from inside the movies folder, you want to extract for. 

Link to comment
Share on other sites

t123thomas

Hi Marauder, Please bare with me I am a total noive with Linux,I checked on youtube how to install bash/Unbutu on Windows 10

Here was my steps >windowss  Update> active developer mode, >  control panel >active program and feature> Turn on  windows feature> check windows subsyetem for linux> Windows store>Install Unbutu

I have Unbutu set up

1 update can be applied immediately.
To see these additional updates run: apt list --upgradable


The list of available updates is more than a week old.
To check for new updates run: sudo apt update


This message is shown once a day. To disable it please create the
/home/emby/.hushlogin file.
emby@DESKTOP-O1TCLEA:~$

 

I dont know what to do next😕

The I did sudo apt update

then I got this

Fetched 23.2 MB in 16s (1451 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
87 packages can be upgraded. Run 'apt list --upgradable' to see them.
emby@DESKTOP-O1TCLEA:~$

Now even confused more LoooL I need to learn Linux

So form here how do I run the movie sub script

 

 

Edited by t123thomas
Link to comment
Share on other sites

marauder
16 hours ago, t123thomas said:

Hi Marauder, Please bare with me I am a total noive with Linux,I checked on youtube how to install bash/Unbutu on Windows 10

Here was my steps >windowss  Update> active developer mode, >  control panel >active program and feature> Turn on  windows feature> check windows subsyetem for linux> Windows store>Install Unbutu

I have Unbutu set up

1 update can be applied immediately.
To see these additional updates run: apt list --upgradable


The list of available updates is more than a week old.
To check for new updates run: sudo apt update


This message is shown once a day. To disable it please create the
/home/emby/.hushlogin file.
emby@DESKTOP-O1TCLEA:~$

 

I dont know what to do next😕

The I did sudo apt update

then I got this

Fetched 23.2 MB in 16s (1451 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
87 packages can be upgraded. Run 'apt list --upgradable' to see them.
emby@DESKTOP-O1TCLEA:~$

Now even confused more LoooL I need to learn Linux

So form here how do I run the movie sub script

 

 

So you'll need to know some linux to get this working. As you're using WSL this could save you a couple steps.

first you need to install the tools you need.

1. In your WSL shell:

apt-get install jq mkvtoolnix -y

2. download the script I attached. Edit lines 12,13 with your Emby internal URL and an emby API key.

Edit line 19 if you want other subtitles besides English and Russian. If you add any languages or change any of them you'll need to edit or add lines between lines 33-38 (i can help if you need)

after that is done you need to copy the file into your Ubuntu WSL installation.

Keep note Capitalization maters in linux, so do spaces. Do if your file has a space in it you need to quote the whole path or escape the space with a \ (example hello\ there or 'hello there') the TAB button in linux is your best friend. It will auto complete out of available options, unlike in windows if there is multiple possibilities it will complete as far as it can until it reaches the blocker. If you hit TAB again it'll show you all the possibles now matching, you can then type until you clear the blocker and hit tab again to complete the rest.

3. Best way to do this is from ubuntu do cp /mnt/c/PATH_TO THE FILE ON YOUR WINDOWS MACHINE /usr/bin

4. in WSL shell:

if you have renamed the script from what I named it, then change the last portion

chmod 755 /usr/bin/movie-subtitle-extract-emby.sh

5. you'll need to change directories to where your movies are stored. in linux cd will change directory and ls will list. IF you're on WSL and your files are mounted on your machine you should be able to see the drive mounted in WSL under /mnt/

6. Once you're in the perticular movie folder of the movie you want to change then simply type the name of the script and enter. movie-subtitle-extract-emby

  • Thanks 1
Link to comment
Share on other sites

t123thomas
1 hour ago, marauder said:

So you'll need to know some linux to get this working. As you're using WSL this could save you a couple steps.

first you need to install the tools you need.

1. In your WSL shell:

apt-get install jq mkvtoolnix -y

2. download the script I attached. Edit lines 12,13 with your Emby internal URL and an emby API key.

Edit line 19 if you want other subtitles besides English and Russian. If you add any languages or change any of them you'll need to edit or add lines between lines 33-38 (i can help if you need)

after that is done you need to copy the file into your Ubuntu WSL installation.

Keep note Capitalization maters in linux, so do spaces. Do if your file has a space in it you need to quote the whole path or escape the space with a \ (example hello\ there or 'hello there') the TAB button in linux is your best friend. It will auto complete out of available options, unlike in windows if there is multiple possibilities it will complete as far as it can until it reaches the blocker. If you hit TAB again it'll show you all the possibles now matching, you can then type until you clear the blocker and hit tab again to complete the rest.

3. Best way to do this is from ubuntu do cp /mnt/c/PATH_TO THE FILE ON YOUR WINDOWS MACHINE /usr/bin

4. in WSL shell:

if you have renamed the script from what I named it, then change the last portion

chmod 755 /usr/bin/movie-subtitle-extract-emby.sh

5. you'll need to change directories to where your movies are stored. in linux cd will change directory and ls will list. IF you're on WSL and your files are mounted on your machine you should be able to see the drive mounted in WSL under /mnt/

6. Once you're in the perticular movie folder of the movie you want to change then simply type the name of the script and enter. movie-subtitle-extract-emby

Thanks

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...