This page shows the source for this entry, with WebCore formatting language tags and attributes highlighted.

Title

Extracting subtitles from an <c>mkv</c> with <c>ffmpeg</c>

Description

I'd watched an excellent movie<fn> 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 <c>mkv</c> 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 <c>ffmpeg</c> from <a href="https://www.reddit.com/r/PleX/comments/eqxfmf/how_to_extract_srt_files_from_mkv_file/ff12htd/" source="Reddit">How to Extract .SRT Files From MKV File</a>. It suggested something like, <code>ffmpeg -i FILENAME.mkv -map 0:s:0 german.srt</code> Once I'd installed <c>ffmpeg</c> with <a href="https://brew.sh/">Homebrew</a>, 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. <pre>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</pre> The <a href="https://ffmpeg.org/ffmpeg.html#Stream-selection"><c>ffmepg</c> documentation</a> isn't particularly illuminating on the <c>-map</c> option, but I finally figured out that the parameter is something like: <ol> The first position seems to be the file selector (you can specify multiple inputs with multiple <c>-i</c> options The second position seems to select the <i>type</i> of stream, where <c>s</c> indicates subtitles (I intuit this because it looks like <c>p</c> indicates <i>programs</i>, according to <a href="https://stackoverflow.com/questions/35667457/ffmpeg-how-to-chose-a-stream-from-all-stream">FFMPEG: How to chose a stream from all stream</a> [sic]) The third position selects the index of the stream within that <i>type</i> </ol> 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. <code>ffmpeg -i FILENAME.mkv -map 0:s:1 german.srt</code> 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 <c>ffmpeg</c> using the well-known package manager <c>brew</c>. <hr> <ft>The movie was <a href="https://www.imdb.com/title/tt11664272/?ref_=fn_al_tt_1" source="IMDb">Oeconomia</a>, which is, honestly, must-see viewing for everyone. Every single person should see this movie, to learn how the macro-level economy really works and how we're being used.</ft>