FFmpeg stands for Fast Forward Moving Picture Experts Group. It’s free, open-source, and one of the most popular multimedia frameworks. It can decode, encode, transcode, mux, demux, stream, filter, and play almost all video and audio formats. It supports most legacy video and audio formats up to the most recent formats.
It’s also very portable and passes FFmpeg’s official testing infrastructure FATE across Linux, Mac OS X, Microsoft Windows, the BSDs, Solaris, and many more running on different build environments, machine architectures, and configurations.
In this tutorial, we’ll explore how to set up FFmpeg on Linux-based distros, then use it to embed subtitles into a video.
Let’s install FFmpeg on Ubuntu-based distribution from the local package manager.
First, let’s update the package registry:
$ sudo apt update
Then we can install FFmpeg:
$ sudo apt install ffmpeg
Finally, to confirm if it’s available, let’s check its version:
$ ffmpeg -version ffmpeg version 4.2.7-0ubuntu0.1 Copyright (c) 2000-2022 the FFmpeg developers built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1) configuration......... truncated
Alternatively, we can use the Pacman package manager to install FFmpeg on Arch Linux.
First, let’s update the package manager:
$ pacman -Syu
After updating, let’s install FFmpeg:
$ pacman -S ffmpeg
After installation, we can confirm if it was successful by checking for the FFmpeg version:
$ ffmpeg --version ffmpeg version n4.2.7 Copyright (c) 2000-2022 the FFmpeg developers
We also need to ensure we have a video file and its corresponding subtitle file. For this tutorial, we’ll be using a sample 20 sec mp4 video.
Here are the contents of the corresponding sample_video_subtitle_ffmpeg.srt subtitle file:
1 00:00:04,700 --> 00:00:05,090 You know what 2 00:00:05,100 --> 00:00:05,990 we should all do. 3 00:00:07,200 --> 00:00:08,690 Go see a musical. 4 00:00:11,900 --> 00:00:12,490 sure, 5 00:00:14,770 --> 00:00:16,530 and you know which one we should see 6 00:00:17,580 --> 00:00:19,880 The 1996, Tony Award winner.
3. How to Add Hard Subtitles
The subtitles filter adds subtitles that are in SRT format, while the ass filter adds subtitles in the ASS format.
3.1. Adding Hard Subtitles in SRT Format
To add SRT subtitles on top of an input video, FFmpeg uses the subtitles filter and the libass library.
The libass library is a portable subtitle renderer that automatically converts input subtitle files to ASS format.
Let’s ensure the libass library is enabled by checking FFmpeg’s version output log:
$ ffmpeg -version ffmpeg version 4.2.7-0ubuntu0.1 Copyright (c) 2000-2022 the FFmpeg developers built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1) configuration: --prefix=/usr ....truncated.... --enable-gnutls --enable-ladspa --enable-libaom [--enable-libass] --enable-libbluray
If libass is enabled, we should see the –enable-libass option from the output.
In addition, the subtitles filter requires a build with libavcodec and libavformat to convert the input subtitle file to ASS format. However, these libraries should be available by default when we install the most recent version of FFmpeg.
Once we’re done with setting up the support libraries, let’s use this command to add subtitles to the sample video file:
$ ffmpeg -i sample_video_ffmpeg.mp4 -vf subtitles=sample_video_subtitle_ffmpeg.srt output_srt.mp4
The –vf option is an alias for -filter:v while subtitles=subtitle.srt represents the subtitles library followed by the name of the subtitle SRT file. The subtitles will be added using the default font settings.
After execution, we’ll get an output_srt.mp4 video file that contains subtitles hard-coded into the video:
In addition, we can style the subtitles on SRT files. However, the styling options in the SRT format are few in comparison to the ASS format.
We can only do basic styling using standard HTML markups such as <i> for italics, <b> for bolding, <u> for underlining, and <font> to change the font face.
For example, let’s bold the first two sentences on the sample subtitle file:
1 00:00:04,700 --> 00:00:05,090 <b>You know what</b> 2 00:00:05,100 --> 00:00:05,990 <b>we should all do.</b> ....truncated....
After saving, let’s add the subtitles to the sample video file, and we should expect to see the first two sentences bolded:
3.2. Adding Hard Subtitles in ASS Format
We can also manually convert the input subtitle SRT file to ASS format, then use the ass filter to add the subtitles.
Let’s convert the sample_video_subtitle_ffmpeg.srt subtitle file to ASS format:
$ ffmpeg -i sample_video_subtitle_ffmpeg.srt output_subtitle.ass ....truncated.... Input #0, srt, from 'sample_video_subtitle_ffmpeg.srt': Duration: N/A, bitrate: N/A Stream #0:0: Subtitle: subrip Output #0, ass, to 'output_subtitle.ass': ....truncated....
Next, let’s run this command to add the subtitles to the sample_video_ffmpeg.mp4 video file:
$ ffmpeg -i sample_video_ffmpeg.mp4 -vf ass=output_subtitle.ass output_ass.mp4
An output_ass.mp4 file will be generated, similar to the output video file we received from the example above.
Additionally, we can add styling to the subtitles attached to the sample video. ASS files offer more styling options in comparison to SRT files.
For example, let’s change the font family to Helvetica and then set the font color to green in the output_subtitle.ass file:
[Script Info] ; Script generated by FFmpeg/Lavc58.54.100 ScriptType: v4.00+ PlayResX: 384 PlayResY: 288 [V4+ Styles] Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding Style: Default,Helvetica,16,&HFF00,&Hffffff,&H0,&H0,0,0,0,0,100,100,0,0,1,1,0,2,10,10,10,0 ....truncated....
Here, we’ve changed the “Style:” line by changing the default Arial font name to Helvetica. We’re also changing the subtitle color to the hex value &HFF00, which represents green.
Saving this file and re-embedding the subtitles to the sample video should show fonts that are red and with a different family:
4. How to Add Soft Subtitles
Adding soft subtitles to videos is easier than hard coding because the whole input video file is not re-encoded.
Let’s use this command to add soft subtitles to the sample_video_ffmpeg.mp4 video file:
$ ffmpeg -i sample_video_ffmpeg.mp4 -i sample_video_subtitle_ffmpeg.srt -c copy -c:s mov_text -metadata:s:s:0 language=eng output_soft_english.mp4
Here, we’re using the -c copy option to specify that the video should not be re-encoded. The -c:s mov_text option sets the input video file format to the MOV_TEXT format.
We’re also using the -metadata:s:s:0 option, which sets metadata for Stream:Subtitle: Number of the stream, starting with 0.
Finally, the language=eng option sets the subtitle language to English.
The resulting ouptut_soft.mp4 file contains the ability to activate the subtitle file or disable it:
We can also add more than one subtitle file to support subtitles in different languages by using the -map option.
Here’s an example of how to add another subtitle.chinese.srt subtitle file to the output file above that already contains English subtitles:
$ ffmpeg -i ouptut_soft_english.mp4 -i subtitle.chinese.srt -map 0 -map 1 -c copy -c:s mov_text -metadata:s:s:1 language=chi output_soft_chinese.mp4
The -map 0 option refers to the input video output_soft_english.mp4 while the -map 1 option refers to the input subtitle file subtitle.chinese.srt. This will give us the ability to choose between English or Chinese subtitles.
In this article, we’ve learned different methods of embedding subtitles into a video using FFmpeg.
First, we explored how to set up FFmpeg on different Linux-based distros. Then we explored how to add hard subtitles in both SRT format and ASS format and style them. The ASS format offers more flexibility in styling the subtitles. The hard subtitles method engraves the subtitles into the video. This is suitable for video players that don’t support subtitles.
Finally, we looked at how to add soft subtitles to video files. Adding soft subtitles is faster since the whole video file is not re-encoded. Soft subtitles are also flexible, as we include several subtitle files to support different languages.