How to Automate Subtitle Management in MPV Player on Linux

Linux offers a variety of media players like VLC, MPV, Haruna, and Celluloid. The last two of them are just front-ends to the stock MPV media player, and this highlights MPV’s versatility in supporting multiple front-ends

And as we’ve already discussed before, you can do a lot of things using the MPV media player if you know how to code, or use the LUA scripts. These scripts can change the interface of MPV, add a menu or button on the OSC, open a file manager in MPV itself, and can even act as a thumbnail previewer for the currently playing video.

In this tutorial, we will discuss and customize the mpv-autosub LUA script which can automatically download and add subtitles to the video currently playing, eliminating the need to search for subtitles online.

Setting Up the Essentials

The script relies on the Python package Subliminal for backend subtitle downloading. To install this package, just open a Terminal window and type the following command:

pip install subliminal
Installing Subliminal Python Package
Installing Subliminal Python Package

Once this package is installed, we are ready to download and enable the script.

How to Download the mpv-autosub Script

Visit the GitHub page of the script and then simply click on the green ‘Code’ button and then click on ‘Download ZIP’.

Download The Code Zip File
Download The Code Zip File

This will download the entire project in the zip format, and to get the autosub.lua script, you will have to extract the ZIP file. Alternatively, if you use the GIT command, then you can directly clone the project by typing the following command in your Terminal:

git clone https://github.com/davidde/mpv-autosub
Cloning The Project Using The Git Command
Cloning The Project Using The Git Command

Personalizing Your mpv-autosub Script

Before we proceed further, we have to first add the location of the subliminal package we installed earlier to this script. First, let’s find where is this package located:

which subliminal
Finding The Location Of A Package Using Which Command
Finding The Location Of A Package Using Which Command

As you can see, this package is located in /home/$USER/.local/bin/subliminal. Now, to add this location to the script, open the LUA script with your favorite text editor, such as Nano or Vim.

# For Nano Users
nano auto-sub.lua

# For Vim Users
vim auto-sub.lua

Once opened, add the location of Subliminal to the beginning of the script.

Add Subliminal Path To The Beginning Of The Script
Add Subliminal Path To The Beginning Of The Script

You can also specify the languages in which you want your subtitles downloaded. If the subtitles are found in the first language, it will not download the second language subtitle unless you press the button assigned to it. Also, scroll down to the bottom if you want to customize the keys assigned to download subtitles.

You Can Change The Key Assigned To Download Subtitles
You Can Change The Key Assigned To Download Subtitles

This is not necessary, but since I use the ‘n’ key to play the next video in the queue, I’m going to assign it to ‘c’. Also, if you have an account on OpenSubtitles, then you can add the login credentials to this script as well if you encounter any error while downloading subtitle.

You Can Add Your Username And Password For Various Websites To Download Subtitles
You Can Add Your Username And Password For Various Websites To Download Subtitles

Once you’re done, just save and exit your text editor. Press Escape and then type :wq if you are using Vim, and press Ctrl+O and Ctrl+X to save and exit out of nano editor.

Positioning the Script in the Right Directory

MPV looks for scripts in the ~/.config/mpv/scripts directory, and we need to create this directory if it does not already exist.

mkdir -pv ~/.config/mpv/scripts/

Once the directory is created, just move the script to this directory by typing the following command:

mv autosub.lua ~/.config/mpv/scripts/
Moving The Script In The Config Directory
Moving The Script In The Config Directory

Now you’re done, just open any media using the MPV media player and see the script in action.

Automatically Searching Subtitles Of Preferred Language
Automatically Searching Subtitles Of Preferred Language

Press ‘b’ or ‘n’ keys to download the subtitles in your first or second preferred language, if it doesn’t start the download automatically for some reason.

Wrapping Up

Because of the customizability of the MPV media player, you can automate a lot of things with the LUA. This is exactly why advanced users on Linux prefer to use MPV rather than VLC. Remember, even subtitles have copyrights. Always download from reputable sources like OpenSubtitles. Any media for which you are downloading the subtitles may or may not be copyrighted, and you are liable for any illegal use of copyrighted media. How else could you leverage MPV’s customizability to enhance your media experience?