11

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.

  • 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. – Faheem Mitha Apr 29 '16 at 0:00
  • 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. – shirish Apr 29 '16 at 8:25
  • Oh. Yes, I always use srt. You could file an issue about it. – Faheem Mitha Apr 29 '16 at 10:09
  • 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. – Faheem Mitha Apr 29 '16 at 10:22
14

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.

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:
  • jasonwryan, I would prefer to keep it simple and hopefully in the .config file. I have also updated my question with more info. – shirish Apr 29 '16 at 8:27
  • @shirish I can't imagine anything simpler; and if you want it in the config file, you'll need to open a feature request. – jasonwryan Apr 29 '16 at 8:46
0

if you want to create something like this, (click on any file associated to mpv and play with subtitles)

inside /usr/share/applications/mpv.desktop:

     ...
     Exec=/home/username/custom_apps/mpv2.sh %F
     ...

then inside the mpv2.sh script:

#!/usr/bin/env bash
subtitles=""
movie="$1"
mdir="${movie%/*}"
name="${movie##*/}"
dir="$(readlink -f "$1")"
mdir="${dir1%/*}"
for file in $mdir/*; do
  if [[ ${file%.*} == ${1%.*} ]]; then
    title="${file%.*}"
    for match in "$title"*; do
      if [[ $match =~ @*.(vtt|ass|srt|sub) ]]; then
        subtitles="$match"
      fi
    done
  fi
done
if [[ -n $subtitles ]]; then
        mpv "$mdir1" --sub-file="$subtitles"
else
    mpv "$name"
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.