Skip to content

Instantly share code, notes, and snippets.

@spirillen
Last active June 10, 2024 23:11
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

ffmprobe -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

Source: https://www.reck.dk/use-ffmpeg-to-add-subtitles-to-video/

@salivity
Copy link

I created an online tool which used this method to add subtitles to a video container, https://davidclews.com/article/280.html you can see the code in the browsers console

@spirillen
Copy link
Author

I created an online tool which used this method to add subtitles to a video container, https://davidclews.com/article/280.html you can see the code in the browsers console

You should mention the rather small limitation, but good initiative.

This tool has a max filesize limit of 2.00 GiB or 2.15 GB

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