Rendering .srt Subtitles with ffmpeg (OS X / Linux)
Some media players handle subtitles well and others… not so much. Rather than leaving it to chance, I like to render the subtitles right into the video. ffmpeg
is an excellent tool for this.
Grab ffmpeg
if you don’t already have it. In OS X you can use brew.
brew install ffmpeg
Or use a package manager in Linux:
apt install ffmpeg
Either way, you want to make sure you have the libass
(Advanced Sub Station) renderer in there. Most package manager versions have this but if you are compiling from source you want to --enable-libass
.
A popular format for subtitles is .srt
which is just a text file. It looks like this:
1
00:02:47,501 --> 00:02:49,502
Did you hear that?2
00:02:49,587 --> 00:02:52,380
They shut down the main reactor.
We'll be destroyed for sure.3
00:02:52,465 --> 00:02:54,049
This is madness.4
00:03:04,060 --> 00:03:05,518
We're doomed....
You’ll want to convert .srt
file into an .ass
file.
ffmpeg -i movie.srt movie.ass
The result of that is another text file with a little more information that looks like this:
[Script Info]
; Script generated by FFmpeg/Lavc58.35.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,Arial,16,&Hffffff,&Hffffff,&H0,&H0,0,0,0,0,100,100,0,0,1,1,0,2,10,10,10,0[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:02:47.50,0:02:49.50,Default,,0,0,0,,Did you hear that?
Dialogue: 0,0:02:49.59,0:02:52.38,Default,,0,0,0,,They shut down the main reactor.\NWe'll be destroyed for sure.
Dialogue: 0,0:02:52.47,0:02:54.05,Default,,0,0,0,,This is madness.
Dialogue: 0,0:03:04.06,0:03:05.52,Default,,0,0,0,,We're doomed....
Now comes the fun part. Render the subtitles into the video with ffmpeg
:
ffmpeg -i movie.mp4 -vf ass=movie.ass movie-subtitled.mp4
Now you should have subtitles rendered into the video!
I’ve had timing problems, for example all titles show up 11.75 seconds too late. The correct fix would be to adjust all the numbers in the .srt
(which I’m sure some subtitling software can do easily) but I just added 11.75 seconds of blank screen to the source, rendered it and then trimmed away the beginning.
Hope you find this helpful.