How to extract subtitle from video using ffmpeg?

113,748

Solution 1

Simple:

ffmpeg -i Movie.mkv -map 0:s:0 subs.srt
  • -i: Input file URL/path.
  • -map: Designate one or more input streams as a source for the output file.
  • s:0: Select the subtitle stream.

Solution 2

-codec:s:0:1 is incorrect. If you use -codec:s:0 then ffmpeg will use the stated codec for the first subtitle stream being passed to the output, if you use -codec:s:1 then it will use it for the second subtitle stream, etc.

You can also use -codec:s to select all output subtitle streams, or -codec:2 to select the third output stream, regardless of what it is.

You are probably confused because the -map option behaves in a different way - there, you have to select which input the selected stream comes from. (so, -map 0:s:0 would take the first subtitle stream from the first input, and feed it to the output). However, -map is for selecting which streams you want to take from the inputs; whereas most of the other options that use stream mapping are for use on the streams after they have been selected (so there's no need to specify which input file they're from), as they are passed to the output.

Solution 3

ffmpeg -i video.mkv subs.srt

Wicked, right?

Worked for me just now, for a bunch of MKVs containing a single SRT stream. Apparently this is the default behavior. My ffmpeg version is 20170516, from ubuntu 16.4

Share:
113,748

Related videos on Youtube

vijay
Author by

vijay

Updated on September 18, 2022

Comments

  • vijay
    vijay almost 2 years

    I am trying to extract subtitle from video as .srt file, I used the following command:

    FFMPEG -i mytestmovie.mkv -vn -an -codec:s:0.1 srt sub.srt
    

    But, I got an error as Unrecognized option codec:s:0:1 So, can you tell me the exact command and how to extract a subtitle as .srt file in video?

  • vijay
    vijay about 11 years
    evilsoup:i would using the following command E:\FFMpeg_Latest>ffmpeg -i E:\Routine\routine.mkv -vn -an -map 0:s:0 srt E:\Routine\sub.srt,it seems an error i got Unable to find a suitable output format for 'srt',can u tell me the command for extracting an subtitle in video...
  • Stuart
    Stuart about 8 years
    I used: ffmpeg -i film.mp4 -vn -an -codec:s srt film.srt that should copy all the subtitles to the srt file.
  • Fabien Snauwaert
    Fabien Snauwaert over 7 years
    This would download the first subtitle track. If there are several, use 0:s:1 to download the second one, 0:s:2 to download the third one, and so on.
  • Fr.
    Fr. over 6 years
    @jm3 Would you happen to know any way to automatically extract all subtitle streams from a file, naming them after their language identifier (e.g. eng, fre, dut etc.)?
  • user25
    user25 about 6 years
    why is it so slow when extracting from big MKV container (~4 gb)?
  • user25
    user25 about 6 years
    @Stuart it doesn't extract all subtitles
  • user25
    user25 about 6 years
    @evilsoup -codec:s so what is the point to use it if it's not going to extract all subtitles at the time? -codec:s:idx seems only this will work? but this way we have to execute this command many times to extract all subtitles
  • user25
    user25 about 6 years
    @evilsoup -codec:s is equal to -codec:s:0 so it doesn't select all subtitles... it will extract first text track
  • Chindraba
    Chindraba over 5 years
    @evilsoup Neither -codec:s nor codec:s:0 "selects" anything. Both tell ffmpeg which codec to apply to an output stream, or streams, of subtitles. The -map option is used to select the stream(s) from the source. To select all the subtitle streams, without knowing how many there are, or their indices, use -map 0:s and if there may, or may not, be any subtitles, the map can be optional, and not cause ffmpeg to fail by using -map 0:s? See the Advanced Options in the ffmpeg docs.
  • Radek Pech
    Radek Pech about 5 years
    To extract more subtitles at once you have to duplicate the -map parameters for each file. Also include -c copy so that it just extracts the file without trying to process it: ffmpeg -i Movie.mkv -c copy -map 0:s:0 subs.01.srt -c copy -map 0:s:1 subs.02.srt. Anyway it will take long time because ffmpeg must read whole video file to find all parts of the subtitle streams.
  • x-yuri
    x-yuri over 2 years
    @RadekPech Why would it try to process it? Or rather I guess it can't not process it since you extract it into a file. As such, what's wrong with the default "processor"?
  • Radek Pech
    Radek Pech over 2 years
    @x-yuri FFMPEG's default action is to process (convert) everything on input. Which means if you don't use -c parameter, it will automatically select a codec (or output format for subtitles - and FFMPEG actually CAN convert subtitle formats!) and process it. It may look OK to convert SRT into SRT, but it may mess up the input file if it contains something bad or unknown (wrong encoding, typo in timecode, overlapping or too short subtitles, etc.). So you should always include -c copy if want a 100% copy of the input.
  • Flash Thunder
    Flash Thunder over 2 years
    doesn't work, says that it matches no stream, and VLC does see subtitles
  • ElektroStudios
    ElektroStudios over 2 years
    What the hell, I tried it on a MP4 file and it extracts the subtitles with EXTRA BIG XXXXL font size (39pt), totally different / bigger size as how it is displayed when playing the mp4 file.