Extracting subtitles with FFmpeg

23.04.2018 @ Tutorials(Command line, Linux)

FFM­peg 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) fea­tures.

FFm­peg 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 sub­ti­tle.

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 Strip­per), 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 sub­ti­tles.

Converting to other formats

While FFm­peg 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.  Sub­ti­tleEd­it 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 Sub­sIm­port addon.

All the posts you can read

2 Comments

  1. Guillermo(2 months ago)

    Estas eraro, -map 0:3 en la mezo: ffm­peg -i input_file -map 0:3 out.srt

    1. Diego Gangl(2 months ago)

      Ah dankon! Mi mal­trafis ke, korek­tos ĝin

Leave a Reply

Your email address will not be published.