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

4

13

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.

Frederiksen answered 19/12, 2013 at 8:41 Comment(6)
Simple ffmpeg -i in.mp4 out.srt works for me correctly. (ffmpeg 2.0.1)Katonah
Try ffmpeg ... -loglevel debug ... to get more info about errorKatonah
I have tries both suggestions, but I cannot identify an obvious mistake. Perhaps this is caused by ffmpeg trying to output chapter markers in the resulting srt file? I have including the detailed debug output.Frederiksen
Can't see anything obvious either, but maybe try a more recent version from ffmpeg.org/download.html and use ffmpeg -i video.mp4 -map 0:s:0 output.srt to only use the first subtitle stream for your output. (By the way, ffmpeg CLI questions are off topic for SO, so I've voted to move it to Super User.)Sandal
Sorry about the off-topic nature of my post. I'll make sure to ask those kinds of questions in SU. I'll try a more recent build of ffmpeg. Cheers.Frederiksen
Please respond to users with an @ (e.g. @slhck), otherwise they don't get a reply. Also, please don't re-ask your question on Super User. Just wait – it will be migrated automatically once enough people vote for it. Thanks.Sandal
F
24

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)

Frederiksen answered 2/1, 2014 at 16:52 Comment(1)
I think that text track is the chapter markers, not sure though.Touter
C
5

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
Crimmer answered 15/10, 2015 at 5:59 Comment(1)
You can remove the resulting font tags that are messing up your formatting by specifying -c:s text like this: ffmpeg -i in.mp4 -map 0:4 -c:s text out.4.srtQuintet
C
3

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....

Crabbed answered 4/2, 2017 at 7:35 Comment(0)
G
0

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.

Gober answered 16/7, 2023 at 20:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.