Subtitles visually integrated into video

Subtitle Handling and Conversion with FFmpeg: A Practical Approach

16 September 2023, 01:14 AM

Understanding FFmpeg

FFmpeg is a comprehensive, cross-platform solution to record, convert, and stream audio and video. It's a command-line tool that is part of many Linux distributions and is also available for Windows and Mac OS X. It includes libavcodec - the leading audio/video codec library.

Essential FFmpeg Commands for Subtitles

Before diving into subtitle handling, ensure you have FFmpeg installed on your system. You can verify the installation by running ffmpeg -version in your terminal.

Embedding Subtitles into a Video

One of the most common tasks is embedding subtitles into a video file. This not only makes the video file more portable but also ensures that the subtitles are displayed correctly across all platforms.

The following command embeds an SRT subtitle file (subtitles.srt) into an MP4 video file (video.mp4), creating a new video file (output.mp4):

ffmpeg -i video.mp4 -i subtitles.srt -c:v copy -c:a copy -c:s mov_text output.mp4

Here, -c:v copy and -c:a copy tell FFmpeg to copy the video and audio streams without re-encoding them, preserving the original quality. -c:s mov_text specifies the codec for the subtitles (in this case, mov_text for MP4 files).

Extracting Subtitles from a Video

Sometimes, you might want to extract subtitles from a video file for editing or translation. The following command extracts subtitles from a video (video.mp4) and saves them as an SRT file (extracted_subtitles.srt):

ffmpeg -i video.mp4 -map 0:s:0 extracted_subtitles.srt

The -map 0:s:0 option specifies that the first subtitle stream of the input file should be used.

Converting Subtitles between Formats

FFmpeg supports a wide range of subtitle formats, including SRT, SSA, and ASS. To convert subtitles from one format to another, you can use the following command:

ffmpeg -i input_subtitles.srt output_subtitles.ass

This command converts an SRT subtitle file (input_subtitles.srt) into an ASS subtitle file (output_subtitles.ass).

Advanced FFmpeg Subtitle Manipulations

Adjusting Subtitle Timing

If your subtitles are out of sync with the video, you can adjust their timing with FFmpeg. For example, to delay the subtitles by 5 seconds, you can use the following command:

ffmpeg -i input.srt -itsoffset 5 -c copy output.srt

Where -itsoffset 5 adds a 5-second delay to the timestamps in the subtitle file.

Burning Subtitles into Video

Sometimes, you may prefer to have the subtitles burned into the video (hardsub), making them a permanent part of the video image. This can be achieved with the following command:

ffmpeg -i video.mp4 -vf subtitles=subtitles.srt output.mp4

The -vf subtitles=subtitles.srt filter graph burns the subtitles from subtitles.srt onto video.mp4, creating a new video file (output.mp4).

Conclusion

FFmpeg provides a powerful set of tools for handling subtitles, from embedding and extracting to converting and adjusting timings. Mastering these FFmpeg commands can significantly improve your video editing workflow and help make your content more accessible to a global audience. Keep experimenting with these options to discover more about what you can accomplish with this versatile tool.

Ready to try us out?

Have questions? Not sure what you need or where to start? We’re here for you.

Let's Talk