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

28,769

Solution 1

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)

Solution 2

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

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

Share:
28,769
rimero
Author by

rimero

Maxime works as a consultant in France. He has been designing and developing applications using Microsoft tools and languages for almost 15 years. As a Solutions Architect, he primarily focuses on systems integration and Service Oriented Architecture but likes to keep an eye on software development and Application Lifecycle Management. Among specialists circles, rumor has it that Maxime loves to tackle obscure problems related to encodings and character sets. He tries and maintain a technical blog on BizTalk and various Microsoft-related technologies.

Updated on February 04, 2020

Comments

  • rimero
    rimero over 4 years

    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.

  • mcdado
    mcdado almost 10 years
    I think that text track is the chapter markers, not sure though.
  • jerblack
    jerblack over 3 years
    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.srt