Render subtitles in video with ffmpeg?

7

I have some caption files that are separate from their respective videos, and I would like to put them together using the code below:

ffmpeg -i videoSemLegenda.mp4 -i legenda.vtt -c copy -c:s mov_text videoComLegenda.mp4

When outputting to videoComLegenda using ffmpeg -i videoComLegenda.mp4 The output of ffmpeg shows that the caption is along with the video, however when trying to play the video in the <video> tag the caption is not shown. How do I force the caption to be written inside the video?

This is the output ffmpeg for a file whose captions were put through the above code:

Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x960 [SAR 1:1 DAR 4:3], 1459 kb/s, 29.97 fps, 29.97 tbr, 16k tbn, 59.94 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(por): Audio: mp3 (mp4a / 0x6134706D), 48000 Hz, stereo, s16p, 256 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
    Stream #0:2(und): Subtitle: mov_text (tx3g / 0x67337874) (default)
    Metadata:
      handler_name    : SubtitleHandler
    
asked by anonymous 15.12.2016 / 17:36

2 answers

5

As you yourself can see in the video information, the caption is coded in the second data track, and it can be played normally by the players who support it, even though it may need to be manually enabled.

Alternatively, you can use the <track> tag of the <video> tag as long as you look at the particularities, limitations, and possible issues of each browser. Example:

  • Some versions of Internet Explorer will not recognize subtitles in the VTT format defined by this tag unless you define the Content-type by server (.htaccess) and similar )
  • Chrome and Opera ignore the default attribute and attempt to match the language of the browser with the caption, probably by taking into account the value of srclang . With me this did not work. The 'CC' icon did not even appear

So, yes, no, if your target audience is heading for a single language, you may want to embed the captions in the video:

ffmpeg -i "path/to/video.mp4" -vf srt="subtitle.srt" "path/to/video-subtitled.mp4"

Or, if you want to style the subtitle with the format AS (Advanced SubStation Alpha):

ffmpeg -i "path/to/video.mp4" -vf ass="subtitle.ass" "path/to/video-subtitled.mp4"

If you need to convert from one format to another, FFMPEG also does this for you:

ffmpeg -i "path/to/subtitle.srt" "path/to/subtitle.ass"

Now if your target audience requires multiple captions in multiple languages (and Youtube is not an option ^ _ ^), you'll even have to use some JavaScript library to handle as many unique features as possible, they have to create the entire UI of the video manually, as in the MDN article

    
23.12.2016 / 16:13
3

% pure% does not support subtitle embedded in files. What you can do is separate the files and call them as below:

<video controls preload="metadata">
   <source src="path/to/videos/my_video_1.mp4" type="video/mp4">
   <track label="Português" kind="subtitles" srclang="pt" src="path/to/captions/my_video_1.vtt" default>
</video>

However, there is a plugin, mp4Box , that can pick up these embedded subtitles. In this link , it talks about how to extract these captions.

    
21.12.2016 / 17:13