Render subtitles in video with ffmpeg?

Asked

Viewed 1,078 times

7

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

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

When getting the output in videoComLegenda using ffmpeg -i videoComLegenda.mp4 ffmpeg output shows that the caption is along with the video, but when trying to play the video in the tag <video> 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 subtitles 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
  • read the answers to this question in the OS: Use ffmpeg to add text Subtitles

  • The commands there passed work the same way, put the subtitle file inside the container but they do not appear in the player

  • I can’t vote to close your question because she’s got an active reward. But I think it’s not part of the scope of the site because it’s not about programming (it’s about using software).

  • I was left with a question, you want to "embeddar" the caption so that it is rendered next to the video, add as track inside the container or simply play a subtitled video using the native <video player> ?

  • But @Luizvieira despite the question referring to a software, the doubt applies rendering a caption inside the video to be displayed within a <video> tag, so I don’t believe it runs away from the scope of the site, and in the OS there are several questions regarding the same software, so I don’t believe there is any problem.

  • @Leandroangelo I want the caption to be rendered together with video when it is played in an HTML player

  • @Leoletto Yes, it applies to rendering the caption inside the video. Or, in other words, a use of ffmpeg that has nothing to do with programming. There may be questions regarding the use of the same software in a programming context (how to automate, how to integrate, etc). If not, I would also vote to close them.

  • Being a Command-line software I think that includes it a little in the programming category.

  • @Leoletto If by any chance your intention is to do this in an automated way within a website (in PHP, maybe?) for the video to be presented correctly to the HTML client, make that clear in the question. Then I think she would be part of the scope of the site. :)

  • I disagree. Otherwise, any software would fall into the programming category.

  • 1

    @Leoletto Cara, have you ever tried to include the caption as an external file even using <track>, as in the element reference? http://www.w3schools.com/TAgs/att_track_kind.asp

  • Yes @Leandroangelo calling the same via an external file works perfectly, but for curiosity I was wondering if it is possible to just embed it in the video

  • 1

    @Leoletto Then just re-encounter the video and actually render the text together with the image. I had to do it about three years ago and with DRM-protected media, I was the only way.

  • Look at this, Leo, how easily you can make your question within the scope, based on what colleague @Leandroangelo mentions. I think it’s just a matter of you editing the question and improving it by including this information from what you’ve already tried (and you might even mention that your search for embedding is about curiosity - although the issue of DRM protection is very relevant). If you edit in this sense, by the way, you get my +1. :)

  • I have no intention of doing this to protect the files, I see no need to edit the question by changing the meaning of it, ta simple and objective so that I also expect a simple answer, and I do not see why the factor of being a question just by "curiosity" should influence the response. Anyway I’m not going to discuss until why it’s forbidden to turn comments into chat. I’m sorry to those who didn’t like the scope covered in the question.

Show 10 more comments

2 answers

5


As you yourself could see in the video information the caption is encoded in the second data track, including, this can be played normally by the players that support it, even if perhaps it is necessary to enable manually.

You have as alternatives to use the marking <track> tag <video> provided you take into account the particularities, limitations and possible problems of each browser. Example:

  • Some versions of Internet Explorer will not recognize subtitles in the VTT format set by this markup unless you set the Content-type by the server (.htaccess and similar)
  • Chrome and Opera ignore attribute default and try to match the browser language with the legend, probably taking into account the value of srclang. With me it didn’t work. The 'CC' icon didn’t even appear

So, just in case, if your target audience is going to a single language, you should embed the subtitles 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 caption, with the format ASS (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 it for you:

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

Now if your target audience requires multiple subtitles, in multiple languages (and Youtube is not a _ option), you will even have to use some Javascript library that accounts for as many particularities as possible, if they have to create the entire video UI manually, as in article of the MDN

3

The HTML5 pure does not support subtitles embedded in the files. What you can do is separate the files and call them as is 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 catch these subtitles embedded. In this link, he talks how to extract these subtitles.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.