Extracting subtitles with FFmpeg

FFMpeg is the swiss army knife of video edi­tors and motion artists world­wide. Let’s take a look at one of the less­er-known (and kin­da unex­pect­ed) features.

FFmpeg can eas­i­ly extract embed­ded sub­ti­tles from videos. This com­mand will grab the default sub­ti­tle track and export it as a srt file:

ffmpeg -i input_file out.srt

What if we want to get a dif­fer­ent sub­ti­tle? First we need to fig­ure out the track num­ber for that sub­ti­tle by run­ning ffm­peg ‑i input_file. You will get an out­put that some­thing like this:

[...]
Stream #0:2(eng): Subtitle: subrip (default)
Metadata:
  title           : English-SRT
Stream #0:3(eng): Subtitle: hdmv_pgs_subtitle
Metadata:
  title           : English-PGS
Stream #0:3(chi): Subtitle: hdmv_pgs_subtitle
Metadata:
  title           : Chinese-PGS
[...]

Notice the tracks are num­bered #0:2, #0:3, etc. This is the val­ue we want to pass over to the map com­mand to select the prop­er subtitle.

ffmpeg -i input_file -map 0:3 out.srt

Removing HTML from subtitles

There are a num­ber of appli­ca­tions and online ser­vices that can strip html tags from sub­ti­tles (like HTML Stripper), but you can also solve this quick­ly with good old sed:

sed -e 's/<[^>]*>//g' subs.srt

You can make an alias to this in your favorite shell so you don’t have to remem­ber or copy/paste it all the time. Keep in mind that the results of sed won’t be as good as a HTML strip­per and com­pa­ny, since this is just a sim­ple regex. That said, it’s usu­al­ly more than enough for subtitles.

Converting to other formats

While FFmpeg can get this done for you with a lim­it­ed num­ber of for­mats (depend­ing on how it was com­piled) there is a bet­ter alter­na­tive.  SubtitleEdit is a handy open source appli­ca­tion that can con­vert between 200+ sub­ti­tle for­mats (and do a lot more of course).

If you want to bring those sub­ti­tles into Blender check out the SubsImport addon.

Batch rendering is complicated

But it does­n’t have to be! Render+ lets you set­up, run batch­es and a whole lot more from the com­fort of Blender’s interface. 


Try out Render+ today!

All the posts you can read
TutorialsCommand line, Linux18.08.2020