Is it possible to extract SubRip (SRT) subtitles from an MP4 video with ffmpeg?

32.4k views Asked by At

I have checked the FFMpeg documentation and many forums and figured out the correct command-line to extract subtitles from an .MP4 video should look like so:

ffmpeg -i video.mp4 -vn -an -codec:s:0 srt out.srt

However, I get the following error, which lends me to question whether this is feasible at all:

Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height

Using ffmpeg -codecs, I can confirm that ffmpeg should be able to encode subrip subtitles.

Using ffmpeg -i video.mp4, I can see that there is two subtitle tracks embedded in the video :

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video.mp4':
...
Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 720x572 [SAR 64:45 DAR 256:143], 1341 kb/s, 25 fps, 25 tbr, 90k tbn, 180k tbc
Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 191 kb/s
Stream #0:2(fra): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 191 kb/s
Stream #0:3(eng): Subtitle: dvd_subtitle (mp4s / 0x7334706D)
Stream #0:4(und): Subtitle: mov_text (text / 0x74786574)

EDIT

I have tested with the simplified command-line shown in the comments but I still get the same error. Here is a link to the detailed verbose output from running the command. I have also tried to completely disable metadata and chapters in the resulting output but that still produces the same error.

4

There are 4 answers

1
Maxime Labelle On

I enventually figured out why I did not succeed:

The specified command-line would have been perfectly fine if the subtitles from the source video were encoded in a text-based representation. However, as can be seen in the output to the ffmpeg -i command-line, the subtitles are encoded in the "dvd_subtitle" format.

The dvd_subtitle format stores bitmaps for each subtitle in the video. Therefore, there is no way ffmpeg would be able to translate the bitmaps into text.

For this task, one has to resort to an OCR-based software which assists a user with the task of identifying each subtitle as text from its bitmap représentation.

(There is a secondary text-based subtitle in the source video, but I don't know where it came from and is not seen by most popular players. For all intents and purposes, this "mov_text" subtitle seems to be a stub placeholder, probably an artifact of the conversion from the original DVD)

0
Donut On

Have been testing FFmpeg video srt-extraction too (on linux). Would you believe a certain mkv-video had 40 srt-subs!

Putting a few things of FFmpeg and linux bash together, I came up with this in order to extract ALL the subs, each as a separate srt:

    echo "Video(s) to extract subs from?"
    echo "(with extension, specific or wildcard, e.g.: *.mkv, *.mp4)" 
    
    read -e -i "*.???" -p "" VID_IN

echo

echo "Just a moment..."
    
    for cnt in {2..10} # loops through nrs 2 to 10. Subtitles often start at '-map 0:2' (if there is 1 video en 1 audio in the vidcontainer!)
    do
    
    for i in ${VID_IN}; do ffmpeg -txt_format text -i "$i" -map 0:${cnt} "${i%.*}_Track${cnt}_subtitle.srt" 2> /dev/null; done
    
    done

echo

echo -e "Ready.
Subtitles extracted."

Don't ask me where I read about '-txt_format text'. Not sure if it is really necessary. I do understand that if you use it, you have to put it in front of the '-i' option. My code counts from the second map, the srt's take the count number of the map as filename 'Track'-number. Video, film subtitles don't always specify the language ('und'). Therefore: track numbers. You can make the number of counts smaller or larger. Larger takes longer of course. If you have a video with subtitles for almost any language, you'll have to go LARGE ;) '2> /dev/null' suppresses FFmpeg's errors. Other subtitle formats? To be seen. Would be saved (or even converted?..) to srt. Can be renamed afterwards. There are other methods, but this works for me for the moment in case of multiple srt tracks. Tested on mp4 and mkv files. Same results.

Cheerio.

0
Manthaar Janyaro On

Try using map option... if there are too many streams in input files....

Syntax would be:

ffmpeg -i video.mp4 -map 0:4 out.srt

Since there are two subtitle streams in your video, first on 0:3 which is dvdsubtitle so cannot be converted to srt so we will convert second subtitle on 0:4 stream which is mov_text and is soft copy of subtitle so can be easily converted....

1
FreeSoftwareServers On

Just FYI, (Can't comment due to rep yet), but extracting SRT from an MP4 will result in a file formatted as MOV_Text not the regular SRT. It will still get added and work but its like changing mp4 to m4v. While it usually works, things don't work the same in the code. Mov_text is horrible for manually adjusting font/size etc etc. Best bet is to download and test an SRT from the web!

This will work, but will result in mov_text coded srt file:

ffmpeg -i in.mp4 out.srt