Play subtitles automatically with mpv

31,156

Solution 1

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.

Solution 2

I use a simple function:

mpvs() {
   local file="$1"
   mpv --sub-file="${file%.*}".srt "$file"
}

If you want to test for the presence of subtitle files with different extensions, you could use a more complex approach:

#!/usr/bin/env bash
# Play subtitles for a film if they exist

movie="$1"
mdir="${movie%/*}"
name="${movie##*/}"

cd "$mdir"
for file in *; do
  if [[ ${file%.*} == ${name%.*} ]]; then
    title="${file%.*}"
    for match in "$title"*; do
      if [[ $match =~ @*.(ass|srt|sub) ]]; then
        subtitles="$match"
      fi
    done
  fi
done

if [[ -n $subtitles ]]; then
  mpv --subfile="$subtitles" "$name"
else
  printf "%s\n" "No subs found, playing film anyway..."
  mpv "$name"
fi

# vim:set sw=2 ts=2 et:
Share:
31,156

Related videos on Youtube

shirish
Author by

shirish

A GNU/Linux and Debian user. Debian user for more than 5 years and yet still feel like a kid who has just started using the system yesterday.

Updated on September 18, 2022

Comments

  • shirish
    shirish almost 2 years

    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.

    • Alen Milakovic
      Alen Milakovic about 8 years
      If the subtitle file has exactly the same name as the media file (aside from the subscript, of course), mpv will use it automatically, I think.
    • shirish
      shirish about 8 years
      It doesn't. Except for .srt it doesn't know about others. This is on 0.14 dunno if this has been fixed in 0.15, 0.16 or 0.17 the latter three releases which are not yet in Debian.
    • Alen Milakovic
      Alen Milakovic about 8 years
      It should be fairly easy to build a Debian package for 0.17 using the 0.14 packaging. The 0.14 packaging has 4 patches against upstream, but they mostly look quite trivial.
  • shirish
    shirish about 8 years
    jasonwryan, I would prefer to keep it simple and hopefully in the .config file. I have also updated my question with more info.
  • jasonwryan
    jasonwryan about 8 years
    @shirish I can't imagine anything simpler; and if you want it in the config file, you'll need to open a feature request.