Skip to content

Instantly share code, notes, and snippets.

@yordanoweb
Last active February 3, 2024 21:55
Show Gist options
  • Save yordanoweb/4decf586f4a9e0d0ec4f3925441344e3 to your computer and use it in GitHub Desktop.
Save yordanoweb/4decf586f4a9e0d0ec4f3925441344e3 to your computer and use it in GitHub Desktop.
Encode with ffmpeg using formatted subtitles

Encode with ffmpeg with formatted subtitles

ffmpeg -i bullet.train.mp4 \
       -f segment -segment_time 00:55:00 -reset_timestamps 1 \
       -c:v mpeg2video -c:a mp2 -b:v 2500k -b:a 128k -s 700x300 -af volume=8.0 \
       -vf "subtitles=filename=bullet.train.srt:force_style='FontSize=26,PrimaryColour=&h00ffff,Bold=1,Shadow=3'" \
       -y bullet.train.%02d.mpg
  • The segment format is to split the output file in chunks of segment_time duration files.

  • The reset_timestamps is to keep subtitles syncronized in the next chunk.

  • The force_style parameter must use single quotes.

  • The .srt file must not be too nested in deep subdirs.

  • Ensure the .srt file is UTF-8 to avoid ffmpeg does not hardcode them into the video file.

  • Avoid spaces and special characters in the subtitles filename.

  • Preferably execute the command in the same dir containing video and subtitle.

  • The ffmpeg parameters order, matters.

  • The hex primary color value is yellow.

Split the file to avoid TV issues with large files

mpgtx -2 bullet.train.mpg -b bullet.train
  • Will split the MPG original file in two files.

But, what about doing everything in a one line command?

ffmpeg -threads 1 -i MissionImpossible_DeadReckoningPartOne2023.mkv \
       -f segment -segment_time 00:55:00 -reset_timestamps 1 \
       -c:v mpeg2video -c:a mp2 -b:v 2500k -b:a 128k -s 700x300 \
       -af volume=8.0 -vf "subtitles=MissionImpossible_DeadReckoningPartOne2023.srt:force_style='FontSize=26,Shadow=3,Bold=1,PrimaryColour=&H00ffff&'" \
       -y mision.imp.3.%02d.mpg

If you want to soften video, use smartblur filter

ffmpeg -threads 1 -i MissionImpossible_DeadReckoningPartOne2023.mkv \
       -f segment -segment_time 00:55:00 -reset_timestamps 1 -c:v mpeg2video \
       -c:a mp2 -b:v 2500k -b:a 128k -s 700x300 -af volume=8.0 \
       -vf "subtitles=MissionImpossible_DeadReckoningPartOne2023.srt:force_style='FontSize=26,Shadow=3,Bold=1,PrimaryColour=&H00ffff&',smartblur=lr=1.0" \
       -y mision.imp.3.%02d.mpg
  • Note that smartblur filter goes inside the same quotes of subtitles filter. If you use separate -vf options ffmpeg will only use the last.
  • Before that, try to convert to UTF-8 the subtitles SRT file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment