Extract Subtitles from Video File with FFMPEG



Published: 2019-08-29 13:06:25 +0000
Categories: BASH,

Language

BASH

Description

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

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"

Requires

License

BSD-3-Clause

Keywords

ffmpeg, subs, subtitles, matroska, mkv, srt, ass,

Latest Posts


Copyright © 2022 Ben Tasker | Sitemap | Privacy Policy
Available at snippets.bentasker.co.uk, http://phecoopwm6x7azx26ctuqcp6673bbqkrqfeoiz2wwk36sady5tqbdpqd.onion and http://snippets.bentasker.i2p
hit counter