Play subtitles automatically with mpv

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.


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: