Shell – Play subtitles automatically with mpv

configurationffmpegmpvshell

Subtitle files come in a variety of formats, from .srt to .sub to .ass and so on and so forth. Is there a way to tell mpv to search for subtitle files alongwith the media files and if it does to start playing the file automatically. Currently I have to do something like this which can be pretty long depending on the filename –

[$] mpv --list-options | grep sub-file                                                                                              
(null) requires an argument
 --sub-file                       String list (default: ) [file]

Look forward to answers.

Update 1 – A typical movie which has .srt (or subscript)

[$] mpv Winter.Sleep.\(Kis.Uykusu\).2014.720p.BrRip.2CH.x265.HEVC.Megablast.mkv                                                    
(null) requires an argument
Playing: Winter.Sleep.(Kis.Uykusu).2014.720p.BrRip.2CH.x265.HEVC.Megablast.mkv
 (+) Video --vid=1 (*) (hevc)
 (+) Audio --aid=1 (aac)
 (+) Subs  --sid=1 'Winter.Sleep.(Kis.Uykusu).2014.720p.BrRip.2CH.x265.HEVC.Megablast.srt' (subrip) (external)
[vo/opengl] Could not create EGL context!
[sub] Using subtitle charset: UTF-8-BROKEN
AO: [alsa] 48000Hz stereo 2ch float
VO: [opengl] 1280x536 yuv420p
AV: 00:02:14 / 03:16:45 (1%) A-V:  0.000

The most interesting line is this :-

(+) Subs  --sid=1 'Winter.Sleep.(Kis.Uykusu).2014.720p.BrRip.2CH.x265.HEVC.Megablast.srt' (subrip) (external)

Now if the file was as .ass or .sub with the same filename, it wouldn't work. I have tried it in many media files which have those extensions and each time mpv loads the video and audio and the protocols but not the external subtitle files.

Update 2 – The .ass script part is listed as a bug at mpv's bts – https://github.com/mpv-player/mpv/issues/2846

Update 3 – Have been trying to debug with help of upstream, filed https://github.com/mpv-player/mpv/issues/3091 for that.

It seems though that it's not mpv which is responsible but ffmpeg (and libavformat) which is supposed to decode the subtitles. Hence have added ffmpeg to it too.

Best Answer

As seen in man mpv:

   --sub-auto=<no|exact|fuzzy|all>, --no-sub-auto
          Load additional subtitle files matching the video filename. The
          parameter specifies how external subtitle files are matched.
          exact is enabled by default.

          no     Don't automatically load external subtitle files.

          exact  Load the media filename with subtitle file extension
                 (default).

          fuzzy  Load all subs containing media filename.

          all    Load all subs in the current and --sub-paths directories.

exact would seem like the appropriate choice, but since it's the default and it doesn't load files like [video name minus extension].srt, fuzzy is the next best bet and it works on my system.

So just echo "sub-auto=fuzzy" >> ~/.config/mpv/mpv.conf.

Related Question