Skip to content

Instantly share code, notes, and snippets.

@spirillen
Last active May 18, 2025 18:31
Show Gist options
  • Save spirillen/af307651c4261383a6d651038a82565d to your computer and use it in GitHub Desktop.
Save spirillen/af307651c4261383a6d651038a82565d to your computer and use it in GitHub Desktop.
Use FFmpeg to add subtitles to video

Use FFmpeg to add subtitles to video

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

To understand the -map option you'll first need to examine your source file by running the video trough ffprobe or ffmpeg

ffprobe video.mp4

OR

ffmpeg -i video.mp4

Then you'll see some lines named "Stream", each stream represent a video, sound and/or subtitle. Streams starting with 0: are video or sound tracks (streams) and streams starting with a number higher than 0 is subtitle (overlays)

To add several subtitles to the same video requires you to add a -c:s mov_text or -c:s srt for each subtitle submitted

If you like to add a title to each soundtrack or subtitle you can use the

-metadata:s: and -metadata:s:s

Example

For English soundtracks you'll use English for language

-metadata:s:0 language=English

For subtitle you have to use the extra s

-metadata:s:s:1 language=English \
-metadata:s:s:2 language=Dansk
@spirillen
Copy link
Author

Ho @hainesr

While it's not essential, feel free to use this as you see fit, as it in fact can help keeping control of the audio|video|subtitle tracks and which one you are handling at the time. Both approaches are correct.

It's important to note that this example utilises the streams from -map 0:0 rather than -map 0:type:track.

Thanks for sharing tho

@hainesr
Copy link

hainesr commented May 18, 2025

Ah, OK, well I confess that the finer points of ffmpeg elude me as it's so deep 😄

Without the extra a in my usage it added the metadata to the video, rather than the audio, but as you say, I was using the -map 0:type:track variant.

Thanks for making this available and thanks for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment