Adding multiple .srt subtitle streams to MP4 with FFMPEG

Viewed 8525

After a lot of trial and error, I managed to add multiple subtitle streams to one MP4. However, when checking the metadata of the MP4, I see some results that I want to change, hoping someone can help me or can explain why it happened.

This is the input (original MP4 to which I added subtitle streams)

$ ffmpeg -i movie.mp4 
ffmpeg version 2.8.11-0ubuntu0.16.04.1 Copyright (c) 2000-2017 the FFmpeg developers
   Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 2048x872 [SAR 1:1 DAR 256:109], 2352 kb/s, 24 fps, 24 tbr, 24 tbn, 48 tbc (default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, 5.1, fltp, 440 kb/s (default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : SoundHandler

Then, I execute the following command. I gathered this information from different sources and I'm not an FFMPEG guru :)

ffmpeg -i movie.mp4 -f srt -i nl.srt -i mk.srt -i sk.srt \
-map 0:0 -map 0:1 -map 1:0 -map 2:0 -map 3:0 \
-c:v copy -c:a copy \
-c:s mov_text -c:s mov_text -c:s mov_text \
-metadata:s:s:0 language=nld -metadata:s:s:1 language=mkd -metadata:s:s:2 language=skv \
NEW.mp4

In a few seconds, the NEW.mp4 is created and when playing the video I get the video and audio that I expect and 3 subtitle streams that can be switched during playback. The subtitles itself are also what I expect to be.

Output of NEW.mp4

$ ffmpeg -i NEW.mp4 
ffmpeg version 2.8.11-0ubuntu0.16.04.1 Copyright (c) 2000-2017 the FFmpeg developers
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 2048x872 [SAR 1:1 DAR 256:109], 2352 kb/s, 24 fps, 24 tbr, 12288 tbn, 48 tbc (default)
Metadata:
  handler_name    : VideoHandler
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, 5.1, fltp, 440 kb/s (default)
Metadata:
  handler_name    : SoundHandler
Stream #0:2(nld): Subtitle: mov_text (tx3g / 0x67337874), 0 kb/s (default)
Metadata:
  handler_name    : SubtitleHandler
Stream #0:3(mkd): Subtitle: mov_text (tx3g / 0x67337874), 0 kb/s
Metadata:
  handler_name    : SubtitleHandler
Stream #0:4(svk): Subtitle: mov_text (tx3g / 0x67337874), 0 kb/s
Metadata:
  handler_name    : SubtitleHandler

I'm not sure if the type of subtitle is the one I should use -> 'mov_text'. If I use the same command but instead of using 'mov_text', I want to use 'srt' or 'subrip', I get some errors.

[mp4 @ 0x7ba2c0] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 @ 0x7ba2c0] Codec for stream 1 does not use global headers but container format requires global headers
[mp4 @ 0x7ba2c0] Could not find tag for codec subrip in stream #2, codec not currently supported in container

The only question that remains for me is how can I make it work with 'srt' or 'subrip' ? In the meantime I will look for solutions and when found, I will put it here.

2 Answers

SRTs are not officially supported in MP4s and ffmpeg will not mux them. They have to be converted to the support Timed Text format that you did with mov_text.


You can get rid of the warning

Codec for stream 0 does not use global headers but container format requires global headers

by adding -flags +global_header.

You can convert the subs to mov_text which is supported for MP4. Like in the command below.

ffmpeg -i input.mp4 -i dutch.srt -i french.srt -i italian.srt -i spanish.srt -i swedish.srt -map 0:v -map 0:a -map 1 -map 2 -map 3 -map 4 -map 5 -c:v copy -c:a copy -c:s mov_text -metadata:s:s:0 language=dut -metadata:s:s:1 language=fre -metadata:s:s:2 language=ita -metadata:s:s:3 language=spa -metadata:s:s:4 language=swe output.mp4

Also you have the option to change the container with ffmpeg to use mkv and then you do not need to convert the subs as mkv supports virtually anything.

ffmpeg -i input.mp4 -sub_charenc ISO-8859-1 -i dutch.srt -sub_charenc ISO-8859-1 -i french.srt -sub_charenc ISO-8859-1 -i italian.srt -sub_charenc ISO-8859-1 -i spanish.srt -sub_charenc ISO-8859-1 -i swedish.srt -map 0:v -map 0:a -map 1 -map 2 -map 3 -map 4 -map 5 -c:v copy -c:a copy -c:s srt -metadata:s:s:0 language=dut -metadata:s:s:1 language=fre -metadata:s:s:2 language=ita -metadata:s:s:3 language=spa -metadata:s:s:4 language=swe output.mkv

In the cases above I am using the -sub_charenc ISO-8859-1 as the srt file is encoded in ISO-8859. You will get the errors like the following if the the srt are not encoded in UTF-8

Invalid UTF-8 in decoded subtitles text; maybe missing -sub_charenc option Error while decoding stream #3:0: Invalid data found when processing input
Related