Extract VTT Subtitles from MKV with FFmpeg
This article provides a straightforward guide on how to extract a WebVTT (.vtt) subtitle track from an MKV video file using FFmpeg in a Linux environment. You will learn how to identify the correct subtitle stream, run the extraction command, and efficiently process multiple files at once using a simple bash loop.
Identify the Subtitle Stream
Before extracting the subtitles, you need to find out which stream index contains the subtitle track you want. MKV files often contain multiple audio, video, and subtitle tracks. You can inspect the file’s metadata by running the following command in your terminal:
ffmpeg -i input.mkvLook through the output for lines labeled Stream
#0:x, where x is a number. You are looking for
a stream that specifies Subtitle. For example, you might
see something like this:
Stream #0:2(eng): Subtitle: subrip (default)
In this case, the subtitle track is at index 2.
Extract the VTT Subtitle Track
Once you know the stream number, you can use FFmpeg to extract it and
convert it to the VTT format. Use the -map flag to select
the specific stream.
Assuming your subtitle stream is 0:2, run the following
command:
ffmpeg -i input.mkv -map 0:2 output.vttHere is what each part of that command does:
-i input.mkv: Specifies the input MKV file.-map 0:2: Directs FFmpeg to only process the stream at index 2.output.vtt: Name of the desired output file. FFmpeg automatically handles the conversion to WebVTT based on this file extension.
Batch Extract Subtitles from Multiple MKV Files
If you have a directory full of MKV files and want to extract the
first subtitle track (typically stream 0:s:0) from all of
them, you can automate the process using a bash for
loop:
for file in *.mkv; do
ffmpeg -i "$file" -map 0:s:0 "${file%.mkv}.vtt"
doneThis script loops through every MKV file in the current folder,
selects the very first subtitle track available (0:s:0),
and saves it as a VTT file matching the original video’s filename.