Removing Undesired Matroska Audio & Subtitle Tracks By Remux

Subtitles

Ever rip a Blu-ray and find that the resulting Matroska defaults to the Russian language audio track? You might want to remove the audio track and the corresponding subtitle tracks. The easiest way I’ve found to do this is to remux the Matroska using the mkvtoolnix suite.

1
$ pkg install mkvtoolnix

This installs a nice toolchain to work with Matroska files. We’re gonna start out by using mkvinfo. It prints information about the Matroska files we want to do surgery on.

1
$ mkvinfo input.mkv

You’re gonna get a bunch of output. Let’s take a closer look at Tracks. It’s a list of tracks that can look something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| + Track
| + Track number: 10 (track ID for mkvmerge & mkvextract: 9)
| + Track UID: 6351025898303726222
| + Track type: audio
| + Default track flag: 0
| + Codec ID: A_AC3
| + Default duration: 00:00:00.032000000 (31.250 frames/fields per second for a video track)
| + Language: spa
| + Name: 1.0 Dolby Digital (1980 Latino dub)
| + Audio track
| + Sampling frequency: 48000
| + Content encodings
| + Content encoding
| + Content compression
| + Algorithm: 3 (header removal)
| + Settings: length 2, data: 0x0b 0x77

The relevant fields for us is Track number, Track type, Language and Name. A lot of times you won’t have the language and in that case you’d have to go by name to find the track number you want to omitt or include. We’ll get to that in a bit but first let’s look at a subtitle track.

1
2
3
4
5
6
7
8
9
10
11
12
| + Track
| + Track number: 19 (track ID for mkvmerge & mkvextract: 18)
| + Track UID: 10046136204725743760
| + Track type: subtitles
| + Default track flag: 0
| + Lacing flag: 0
| + Codec ID: S_HDMV/PGS
| + Language: spa
| + Name: Spanish-FORCED-PGS
| + Content encodings
| + Content encoding
| + Content compression

Very similar and the relevant fields are the same.

So how do we remove audio or subtitle tracks from out Matroska? This can be approached in two different ways. Either you specify what tracks you want to include in your new file or you specify what tracks you want to exclude. This can also be done in different ways. Let’s look at the most basic example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|+ Tracks
| + Track
| + Track number: 1 (track ID for mkvmerge & mkvextract: 0)
| + Track type: audio
| + Default track flag: 1
| + Language: spa
| + Name: Español
| + Track
| + Track number: 2 (track ID for mkvmerge & mkvextract: 1)
| + Track type: audio
| + Language: eng
| + Name: English
| + Track
| + Track number: 3 (track ID for mkvmerge & mkvextract: 2)
| + Track type: subtitles
| + Language: eng
| + Name: English
| + Track
| + Track number: 4 (track ID for mkvmerge & mkvextract: 3)
| + Track type: subtitles
| + Language: spa
| + Name: Español

Now, perhaps the default audio track is in Spanish but you only speak English so you want to nuke everything Spanish about this file instead of fiddling with default track flags.

Since we have Language on both out Spanish tracks we can just exclude them by muxing with the mkvmerge tool:

1
$ mkvmerge -o output.mkv -a '!spa' -s '!spa' input.mkv

In this case -o means output, -a means audio tracks, '!spa' means exclude tracks with language code spa and -s means subtitle tracks.

This is a good option if there are audio or subtitle tracks of several languages that you want to keep and you only want to nuke Spanish.

Similarly, if we only want the English tracks we can just include them in particular:

1
$ mkvmerge -o output.mkv -a 'eng' -s 'eng' input.mkv

This will remove any other tracks.

As I said before, you might not have the language code specified in the track. Say it instead looks like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|+ Tracks
| + Track
| + Track number: 1 (track ID for mkvmerge & mkvextract: 0)
| + Track type: audio
| + Language: eng
| + Name: English
| + Track
| + Track number: 2 (track ID for mkvmerge & mkvextract: 1)
| + Default track flag: 1
| + Track type: subtitles
| + Name: Simplified Chinese
| + Track
| + Track number: 3 (track ID for mkvmerge & mkvextract: 2)
| + Track type: subtitles
| + Name: English

By assumption we got automatically enabled Chinese subtitles. Say we want to get rid of the track completely. This is where the track number comes in handy, as hinted by (track ID for mkvmerge & mkvextract: 1). Remember to use the number within the parentheses which is going to be one less than the track number.

1
$ mkvmerge -o output.mkv -s 2 input.mkv

This is going to mux a new Matroska with only subtitle with track number 3 from the input. Since we’re using the -s flag we’re not touching the audio tracks. If you want to specify multiple tracks you can comma separate them like 3,4,6.

An added bonus is that you save space from remuxing away undesired tracks.