1. Overview

Matroska, commonly known as an MKV file, is a multimedia format that encapsulates audio, video, and subtitle files into a complete movie file.

In this tutorial, we’ll see how to extract the subtitle file(s) from an MKV file.

All commands in this guide have been tested on Debian 12 running MKVToolNix 74.0.0 and FFmpeg 5.1.4.

2. Sample File

Let’s use the sample file (11MB) from the Matroska website. Matroska is a container. Therefore, the sample file consists of:

  • one video file
  • one audio file
  • two subtitles, one in English and one in German

Since the file is a video file, the file extension is .mkv. However, Matroska actually has various file types based on its use. For example:

  • .mkv for video
  • .mka for audio
  • .mks for subtitle
  • .mk3d for 3D video

In the next sections, we’ll explore how to locate the subtitles in an MKV file and extract them.

3. Using MKVToolNix

MKVToolNix is a package containing a set of command-line tools to inspect, edit, and create Matroska files.

As a result, the package includes four binaries:

  • mkvmerge: to create Matroska files
  • mkvinfo: to get information about the tracks
  • mkvextract: to extract tracks
  • mkvpropedit: to edit header and chapter information or attachment properties

We’ll use the mkvinfo and mkvextract commands to get the subtitle track number and extract the subtitle, respectively.

3.1. Installation

The MKVToolNix package is available on the Debian official repository under its canonical name, mkvtoolnix:

$ apt search mkvtoolnix
Sorting... Done
Full Text Search... Done
mkvtoolnix/stable,now 74.0.0-1 amd64
Set of command-line tools to work with Matroska files

mkvtoolnix-gui/stable,now 74.0.0-1 amd64
Set of tools to work with Matroska files - GUI frontend

We can install either the text mode or the GUI mode. The GUI mode is a program that, in turn, also automatically installs the text mode.

Let’s install the text mode:

$ sudo apt install mkvtoolnix
$ mkvinfo --version
mkvinfo v74.0.0 ('You Oughta Know') 64-bit

Once the installation is finished, we should be able to check the software version using one of the tools, such as mkvinfo.

3.2. Finding the Subtitle Track Number

An MKV file may contain multiple files, each identified by a track number.

Let’s view them using mkvinfo:

$ mkvinfo vsshort-vorbis-subs.mkv
...
|+ Tracks
| + Track
| + Track number: 1 (track ID for mkvmerge & mkvextract: 0)
| + Track type: video
...
| + Track
| + Track number: 2 (track ID for mkvmerge & mkvextract: 1)
| + Track type: audio
...
| + Track
| + Track number: 3 (track ID for mkvmerge & mkvextract: 2)
| + Track type: subtitles
...
| + Track
| + Track number: 4 (track ID for mkvmerge & mkvextract: 3)
| + Track type: subtitles
...

We need to know the position or the track number of the subtitle before extracting it. Hence, referring to the output above, we see that the subtitle track numbers are 3 (English) and 4 (German).

However, the track IDs for the mkvmerge and mkvextract commands start from an index of 0. Therefore, when using these two commands, we need to refer to the subtitle track IDs as 2 and 3, respectively.

3.3. Extracting the Subtitle

Some MKV files may contain multiple subtitles, so we need to choose which one we want to extract.

Let’s extract the English subtitle using mkvextract and then store it in an SRT file named sub-en.srt:

$ mkvextract tracks vsshort-vorbis-subs.mkv 2:sub-en.srt
Extracting track 2 with the CodecID 'S_TEXT/UTF8' to the file 'sub-en.srt'. Container format: SRT text subtitles
Progress: 100%
$ head sub-en.srt
1
00:00:04,700 --> 00:00:06,736
where are you going so early?
...

Let’s break down the command:

  • tracks: tells mkvextract to run in tracks extraction mode
  • vsshort-vorbis-subs.mkv: the MKV filename
  • 2:sub-en.srt: the digit 2 is the track ID and is followed by a colon (:) and then the filename to store the subtitle

We then used the head command to take a quick peek at the first part of the file.

Additionally, if we want to extract the German subtitle, we can simply change the track ID to 3 and store the subtitle in a different file, such as sub-de.srt:

$ mkvextract tracks vsshort-vorbis-subs.mkv 3:sub-de.srt
Extracting track 3 with the CodecID 'S_TEXT/UTF8' to the file 'sub-de.srt'. Container format: SRT text subtitles
Progress: 100%
$ head sub-de.srt
1
00:00:04,700 --> 00:00:06,736
Wo gehst du so früh hin?
...

At this point, we’ve successfully exported the subtitles from the MKV file using MKVToolNix.

4. Using FFmpeg

FFmpeg is a popular framework for manipulating multimedia files.

4.1. Installation

The FFmpeg package is available on the Debian official repository under its canonical name, ffmpeg.

Let’s install it:

$ sudo apt install ffmpeg
$ ffmpeg -version
ffmpeg version 5.1.4-0+deb12u1 Copyright (c) 2000-2023 the FFmpeg developers
...

Once installed, we should be able to check its version number.

4.2. Finding the Subtitle Track Number

Similar to mkvextract, we need to find the subtitle track ID or the subtitle stream before extracting it:

$ ffmpeg -i vsshort-vorbis-subs.mkv 2>&1 | grep Stream
Stream #0:0: Video: msmpeg4v3 (DIV3 / 0x33564944), yuv420p, 640x352, SAR 1:1 DAR 20:11, 23.98 fps, 23.98 tbr, 1k tbn (default)
Stream #0:1: Audio: vorbis, 48000 Hz, stereo, fltp (default)
Stream #0:2: Subtitle: subrip (default)
Stream #0:3: Subtitle: subrip

Let’s break down the command:

  • -i vsshort-vorbis-subs.mkv: indicates the input filename
  • 2>&1: redirects stderr to stdout because ffmpeg outputs to stderr instead of stdout
  • |: pipe operator
  • grep Stream: the grep command searches the ffmpeg output for the Stream pattern

From the command output, we can see that the MKV file contains two subtitles on input Stream #0: index 2 and 3.

Stream #0 represents the first input file. This is because ffmpeg can accept multiple input files for various media manipulations, such as concatenating videos.

4.3. Extracting the Subtitle

Let’s extract the English subtitle:

$ ffmpeg -i vsshort-vorbis-subs.mkv -map 0:s:0 sub-en-ffmpeg.srt
$ head sub-en-ffmpeg.srt
1
00:00:04,700 --> 00:00:06,736
where are you going so early?
...

We used the head command to print out the first part of the subtitle file to ensure the subtitle was exported correctly.

Let’s break down the ffmpeg command.

First, we have the -i option, followed by a filename, vsshort-vorbis-subs.mkv. This indicates the input filename.

Then, we have the -map 0:s:0 option. This specifies which subtitle stream to include in the output:

  • 0: input file index
  • s: subtitle stream
  • 0: subtitle stream index

Finally, we have the output filename: sub-en-ffmpeg.srt.

In case we want to extract the German subtitle, we just need to change the subtitle stream index to 1, and store the output to a different file, such as sub-de-ffmpeg.srt:

$ ffmpeg -i vsshort-vorbis-subs.mkv -map 0:s:1 sub-de-ffmpeg.srt
$ head sub-de-ffmpeg.srt
1
00:00:04,700 --> 00:00:06,736
Wo gehst du so früh hin?
...

Unsurprisingly, the ffmpeg command also extracted the German subtitle successfully.

5. Conclusion

In this article, we learned how to export subtitles from an MKV file using MKVToolNix and FFmpeg. Both tools require the subtitle index, and we can find the index using the mkvinfo command or the ffmpeg -i command.

Afterwards, we used the mkvextract command and ffmpeg -map command to extract the subtitle.

Finally, we used the head command to verify that all subtitle files were exported correctly.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments