Extracting subtitles from an mkv
with ffmpeg
Published by marco on
I’d watched an excellent movie[1] that was primarily in German but had some English parts, with hard-coded English subtitles and soft German subtitles plastered on top of that. I wanted to cite a bunch of interesting sections, so I looked for the subtitles online. Only the English subtitles are available, which I didn’t want. I liked the German formulation and wanted to cite that.
Well, I have the subtitles: they’re just trapped in the mkv
file. I figured that there was some way of extracting them, but a search turned up a lot of pre-compiled and sketchy-looking software whose veracity I couldn’t adequately validate. I want the subtitles, but I don’t want to get a virus or crypto-locked.
I got a good hint to use ffmpeg
from How to Extract .SRT Files From MKV File (Reddit). It suggested something like,
ffmpeg -i FILENAME.mkv -map 0:s:0 german.srt
Once I’d installed ffmpeg
with Homebrew, I was able to extract a subtitle stream. Unfortunately, it was kind of short, so I’d grabbed the wrong stream.
Part of the output of the command above is a list of available streams, shown below.
Stream #0:0: Video: h264 (Main), … (default) Metadata: DURATION : 01:25:55.332000000 Stream #0:1(ger): Audio: aac (LC), 48000 Hz, stereo, fltp (default) Metadata: title : Stereo DURATION : 01:25:55.285000000 Stream #0:2(ger): Subtitle: ass Metadata: title : German forced DURATION : 01:03:24.130000000 Stream #0:3(ger): Subtitle: ass Metadata: title : German DURATION : 01:25:43.890000000 Stream #0:4(ger): Subtitle: ass Metadata: title : German SDH DURATION : 01:25:43.890000000
The ffmepg
documentation isn’t particularly illuminating on the -map
option, but I finally figured out that the parameter is something like:
- The first position seems to be the file selector (you can specify multiple inputs with multiple
-i
options - The second position seems to select the type of stream, where
s
indicates subtitles (I intuit this because it looks likep
indicates programs, according to FFMPEG: How to chose a stream from all stream [sic]) - The third position selects the index of the stream within that type
Armed with this information, I was able to select the second subtitle stream, which is the full German subtitles rather than just the German subtitles for the English parts.
ffmpeg -i FILENAME.mkv -map 0:s:1 german.srt
This gave me the desired subtitles in seconds.
Happily, I have what I want and I didn’t have to install any sketchy tools that were installed in an unvetted binary. Instead, I’m comfortable installing the well-known tool ffmpeg
using the well-known package manager brew
.