231

I am trying to add text subtitles to an .mp4 container using ffmpeg:

ffmpeg -i input.mp4 -i input.srt -map 0.0 -map 0.1 -map 1.0 output.mp4

When I am trying to run this line, it gives me an error :

Nmber of stream maps must match number of output streams.

If I try to change the mp4 to mkv (although mp4 supports text subtitles), like this:

ffmpeg -i input.mp4 -i input.srt -map 0.0 -map 0.1 -map 1.0 output.mkv

It correctly maps the streams, but gives an error :

Encoder (codec id 94210) not found for output stream #0.2

When I launch

ffmpeg -codecs

I can see that srt codec is supported as decoder and encoder, however I am not sure what is used for mp4 and mkv subs encoding, and whether I need to switch it on or compile separately.

2
  • 1
    It looks like this question has run its course and remains useful, can this be moved to Super User?
    – John
    Commented May 30, 2023 at 16:14
  • I found baeldung.com/linux/subtitles-ffmpeg very helpful.
    – qknight
    Commented May 15 at 10:46

9 Answers 9

349

NOTE: This solution adds the subtitles to the video as a separate optional (and user-controlled) subtitle track.

ffmpeg -i infile.mp4 -i infile.srt -c copy -c:s mov_text outfile.mp4

-vf subtitles=infile.srt will not work with -c copy


The order of -c copy -c:s mov_text is important. You are telling FFmpeg:

  1. Video: copy, Audio: copy, Subtitle: copy
  2. Subtitle: mov_text

If you reverse them, you are telling FFmpeg:

  1. Subtitle: mov_text
  2. Video: copy, Audio: copy, Subtitle: copy

Alternatively you could just use -c:v copy -c:a copy -c:s mov_text in any order.

11
  • 23
    This method add subtitle to file as one of stream, so need player support to show subtitle(such as VLC)
    – BollMose
    Commented Sep 28, 2015 at 7:59
  • 5
    Yes @sunk818 that's what this means. This adds the subtitle as a track that can be enabled or disabled as long as the player supports it. Mr. Hyde and Paul ffmpeg can also add the subtitles ontop of the video itself, and in those cases you would control things like font and positioning.
    – Boushley
    Commented Feb 10, 2016 at 17:07
  • 6
    .srt files must be imported with -c:s copy not with -c:s mov_text.
    – GetFree
    Commented Jun 22, 2019 at 18:27
  • 2
    What's mov_text for?
    – user3310334
    Commented Sep 12, 2022 at 14:09
  • 3
    ffmpeg -i infile.mp4 -i infile.srt -c copy -c:s mov_text metadata:s:s:0 language=eng outfile.mp4 to set the metadata for the first subtitle stream, and language=eng specifies that the language of the subtitle track is English
    – TunaFFish
    Commented Dec 25, 2023 at 10:18
183

NOTE: This solution "burns the subtitles" into the video, so that every viewer of the video will be forced to see them.

If your ffmpeg has libass enabled at compile time, you can directly do:

ffmpeg -i mymovie.mp4 -vf subtitles=subtitles.srt mysubtitledmovie.mp4

This is the case e.g. for Ubuntu 20.10, you can check if ffmpeg --version has --enable-libass.

Otherwise, you can the libass library (make sure your ffmpeg install has the library in the configuration --enable-libass).

First convert the subtitles to .ass format:

ffmpeg -i subtitles.srt subtitles.ass

Then add them using a video filter:

ffmpeg -i mymovie.mp4 -vf ass=subtitles.ass mysubtitledmovie.mp4
10
  • 64
    This will "burn them into" the video, meaning you can't turn them off in the player. This is different to adding them as a subtitle stream which can be read by the player and displayed if the viewer wants them.
    – stib
    Commented Nov 14, 2014 at 9:03
  • 4
    Thanks very much for this solution. Is there anyway to specify the size of the characters? Commented Apr 12, 2015 at 0:26
  • 2
    Take a look at this answer: stackoverflow.com/questions/21363334/…. You will probably want to set the font in the subtitle file itself, otherwise using the subtitle filter, you could force_style to set the Font: ffmpeg.org/ffmpeg-filters.html#subtitles-1
    – HdN8
    Commented Apr 27, 2015 at 16:00
  • 1
    Install libass extensions if missing on Debian systems with `apt update && apt install libass-dev". Commented Sep 6, 2019 at 21:51
  • 4
    Notably you can do this in one step: ffmpeg -i mymovie.mp4 -vf subtitles=subtitles.srt mysubtitledmovie.mp4. You still need ffmpeg compiled with libass though.
    – Migwell
    Commented Apr 13, 2020 at 4:59
94

You are trying to mux subtitles as a subtitle stream. It is easy but different syntax is used for MP4 (or M4V) and MKV. In both cases you must specify video and audio codec, or just copy stream if you just want to add subtitle.

MP4:

ffmpeg -i input.mp4 -f srt -i input.srt \
-map 0:0 -map 0:1 -map 1:0 -c:v copy -c:a copy \
-c:s mov_text output.mp4

MKV:

ffmpeg -i input.mp4 -f srt -i input.srt \
-map 0:0 -map 0:1 -map 1:0 -c:v copy -c:a copy \
-c:s srt output.mkv
10
  • 5
    Is it possible to add more than one subtitle? Will this softsub be recognized as language unknown?
    – Patrick
    Commented Jan 12, 2016 at 5:37
  • 1
    Yes, it is. I've just tested it for MKV:
    – dotokija
    Commented Jan 13, 2016 at 16:06
  • 11
    First add another input: -i input2.srt. Second, map that as 2nd stream: -map 2:0. Finally, select encoder for 2nd subtitle stream (the same as the first one): -c:s srt. The complete example\ ffmpeg -i input.mp4 -f srt -i input.srt -i input2.srt\ -map 0:0 -map 0:1 -map 1:0 -map 2:0 -c:v copy -c:a copy \ -c:s srt -c:s srt output.mkv
    – dotokija
    Commented Jan 13, 2016 at 16:15
  • 31
    And to add language metadata (insert before output file) "-metadata:s:s:0 language=eng"
    – Patrick
    Commented Jan 14, 2016 at 13:00
  • 2
    @user136036 If you don't use mapping, then adding additional subtitles would overwrite the existing ones.
    – m33ts4k0z
    Commented May 13, 2022 at 21:34
14

MKV container supports video and audio codecs Virtually anything and also supports subtitles and DVD menus. So you can just copy codecs from input video to output video with MKV container with subtitles. First you should convert SRT to ASS subtitle format

ffmpeg -i input.srt input.ass

and embed ASS subtitles to video

ffmpeg -i input.mp4 -i input.ass -c:v copy -c:a copy -c:s copy -map 0:0 -map 0:1 -map 1:0 -y out.mkv

Also worked with VMW file.

ffmpeg -i input.wmv -i input.ass -c:v copy -c:a copy -c:s copy -map 0:0 -map 0:1 -map 1:0 -y out.mkv

see the wiki page Comparison of container formats

1
  • 1
    H.V. not really relevant, it's always welcome to see more solutions than one.
    – JasonXA
    Commented Apr 12, 2015 at 11:54
13

ffmpeg supports the mov_text subtitle encoder which is about the only one supported in an MP4 container and playable by iTunes, Quicktime, iOS etc.

Your line would read:

ffmpeg -i input.mp4 -i input.srt -map 0:0 -map 0:1 -map 1:0 -c:s mov_text output.mp4

1
  • 1
    note that this will re-encode the video and audio. Use -c:v copy -c:a copy to copy them.
    – stib
    Commented Sep 17, 2021 at 7:03
8

I tried using MP4Box for this task, but it couldn't handle the M4V I was dealing with. I had success embedding the SRT as soft subtitles with ffmpeg with the following command line:

ffmpeg -i input.m4v -i input.srt -vcodec copy -acodec copy -scodec copy -map 0:0 -map 0:1 -map 1:0 -y output.mkv

Like you I had to use an MKV output file - I wasn't able to create an M4V file.

6

I will provide a simple and general answer that works with any number of audios and srt subtitles and respects the metadata that may include the mkv container. So it will even add the images the matroska may include as attachments (though not another types AFAIK) and convert them to tracks; you will not be able to watch but they will be there (you can demux them). Ah, and if the mkv has chapters the mp4 too.

ffmpeg -i <mkv-input> -c copy -map 0 -c:s mov_text <mp4-output>

As you can see, it's all about the -map 0, that tells FFmpeg to add all the tracks, which includes metadata, chapters, attachments, etc. If there is an unrecognized "track" (mkv allows to attach any type of file), it will end with an error.

You can create a simple batch mkv2mp4.bat, if you usually do this, to create an mp4 with the same name as the mkv. It would be better with error control, a different output name, etc., but you get the point.

@ffmpeg -i %1 -c copy -map 0 -c:s mov_text "%~n1.mp4"

Now you can simply run

mkv2mp4 "Video with subtitles etc.mkv"

And it will create "Video with subtitles etc.mp4" with the maximum of information included.

2
  • thanks , your answer is so helpful Commented Apr 25, 2022 at 16:25
  • Yes, exactly what ffmpeg settings I would expect. Except, the input TS file has Stream #0:3[0x1200](eng): Subtitle: hdmv_pgs_subtitle ([144][0][0][0] / 0x0090) Stream #0:4[0x1201](eng): Subtitle: hdmv_pgs_subtitle ([144][0][0][0] / 0x0090), 1920x1080 and then complains about<br/> Subtitle encoding currently only possible from text to text or bitmap to bitmap Your process above would make more sense to me if you went TS->MKV->MP4??? Commented Jan 16, 2023 at 22:56
-1

Simple Example:

videoSource=test.mp4
videoEncoded=test2.mp4
videoSubtitle=test.srt
videoFontSize=24
ffmpeg -i "$videoSource" -vf subtitles="$videoSubtitle":force_style='Fontsize="$videoFontSize"' "$videoEncoded"

Only replace the linux variables

1
  • 3
    Care to elaborate?
    – RamenChef
    Commented Nov 18, 2016 at 16:52
-6

This is the reason why mkv is such a good container, especially now that it's mature:

mkvmerge -o output.mkv video.mp4 subtitle.srt
1
  • 9
    Care to comment on why your approach would fix the OP's problem?
    – mjuarez
    Commented Apr 20, 2015 at 0:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.