Published on

Add Subtitle to Video File with FFmpeg

Authors
  • avatar
    Name
    Hieu Cao
    Twitter

Why Add Subtitles to Videos?

Subtitles enhance videos by:

  • Accessibility: Make content accessible to people with hearing impairments.
  • Localization: Support multiple languages for a global audience.
  • Improved Engagement: Allow viewers to understand videos in noisy environments or without sound.

Adding Subtitles to Videos

Using Hardcoded Subtitles

Hardcoding subtitles means embedding them into the video permanently. Use this method if you want the subtitles to appear in all playback scenarios.

ffmpeg -i video.mp4 -vf subtitles=subtitles.srt output.mp4
  • -i video.mp4: Input video file.
  • -vf subtitles=subtitles.srt: Apply the subtitles filter with the SRT file.
  • output.mp4: Output file with hardcoded subtitles.

Example with Custom Font and Styling

ffmpeg -i video.mp4 -vf "subtitles=subtitles.ass" output.mp4
  • ASS (Advanced SubStation Alpha) allows more styling options like font size, color, and positioning.

Using Soft Subtitles

Soft subtitles are added as separate tracks in the video file. This method allows viewers to toggle subtitles on or off.

ffmpeg -i video.mp4 -i subtitles.srt -c:v copy -c:a copy -c:s mov_text output.mp4
  • -i subtitles.srt: Input subtitle file.
  • -c:v copy: Copy the video stream without re-encoding.
  • -c:s mov_text: Use the MOV_TEXT codec for subtitles (compatible with MP4).

Verifying Subtitles

After adding subtitles, verify them using FFprobe:

ffprobe -i output.mp4

Look for a subtitle stream in the output file. Example output:

Stream #0:2: Subtitle: mov_text (eng)

Converting Subtitles to Compatible Formats

If your subtitles are not in SRT format, convert them using FFmpeg:

ffmpeg -i subtitles.vtt subtitles.srt

This command converts WebVTT subtitles to SRT format.


Best Practices

  1. Use Appropriate Formats: Ensure your subtitles are in SRT or ASS format for compatibility.
  2. Preview the Video: Check the output file to confirm that subtitles are correctly added.
  3. Choose Hardcoded or Soft Subtitles: Decide based on whether viewers should have the option to toggle subtitles.

Conclusion

Adding subtitles with FFmpeg is a simple and efficient way to enhance the accessibility and usability of your videos. Whether you prefer hardcoded subtitles for universal playback or soft subtitles for user control, FFmpeg provides the tools to get the job done. Start adding subtitles to your videos today!