Extract Subtitles from Video File with FFMPEG (BASH)
Matroska (MKV) containers are pretty good, as they can contain multiple subtitle tracks allowing you to select the desired subtitle language from your player
Player support isn't always so great though, so sometimes you want to be able to extract the subtitles out to a SRT file so that it can the be burned into a MP4, or loaded alongside
This command uses ffmpeg to extract subtitle tracks from a video contianer (doesn't necessarily need to be MKV) to a SRT file
Details
- Language: BASH
- License: BSD-3-Clause
Snippet
# Use ffprobe to list subtitles
ffprobe -loglevel error -select_streams s -show_entries stream_tags=language -of csv=p=0 $FILE
# Using a 0 indexed list, select the desired subtrack by id and write out to a file
ffmpeg -i "$FILE" -map 0:s:$SUB_TRACK "$OUT.srt"
Usage Example
# List the sub tracks
ffprobe -loglevel error -select_streams s -show_entries stream_tags=language -of csv=p=0 Big_Buck_Bunny.mkv
eng
jap
# Extract each
ffmpeg -i "Big_Buck_Bunny.mkv" -map 0:s:0 "Big_Buck_Bunney.eng.srt";
ffmpeg -i "Big_Buck_Bunny.mkv" -map 0:s:1 "Big_Buck_Bunney.jap.srt";
# Bonus: Some subtitle tracks contain HTML formatting. Strip HTML formatting from the file
sed -i -e 's/<[^>]*>//g' "Big_Buck_Bunney.eng.srt"
sed -i -e 's/<[^>]*>//g' "Big_Buck_Bunney.jap.srt"