Add subtitles to a video in Linux

Let's hardcode some .srt/.ass subtitles into a video with ffmpeg.

2019-03-14
2019-07-30
linux
video
subtitles

.srt subtitles

Ever wondered how to hardcode .srt subtitles into a video?
Or how to modify the font size before fusing them?

These notes will show exactly how.

From ISO-8859-1 to UTF-8

For the process to be successful, we need the .srt file to be UTF-8 encoded.
Spanish subtitles files are commonly encoded as ISO-8859-1.

So, is it ISO-8859-1 or UTF-8?

We can find out with:

file some.srt

If it’s UTF-8 encoded, you’ll see something like:

some.srt: UTF-8 Unicode text, with CRLF line terminators

If it’s ISO-8859-1 it’d be:

some.srt: ISO-8859 text, with CRLF line terminators

To change the encoding for a file from ISO-8859-1 to UTF-8, use:

iconv -f ISO-8859-1 -t UTF-8 some.srt > utf8.srt

Now your UTF-8 encoded subs will be in the utf8.srt file.

.ass subtitles

The downside to using a pure .srt file, is that you cannot tweak the subtitles.

Convert .srt to .ass

That’s why it’s better to convert them to .ass:

ffmpeg -i utf8.srt subs.ass

Tweak the subtitles

To format the subtitles, just open the .ass file in any text editor and modify the font size, etc. —I always change the font size from 16 to 24.

nano subs.ass

Hardcode the subtitles

Save and exit, then hardcode the subtitles with:

ffmpeg -i video.mp4 -vf ass=subs.ass subtitled-video.mp4

That’s it! :tada:

P.S. The process is very CPU intensive, but uses all your available cores.
The more you have the better!

I still want to hardcode .srt subtitles

Wait, before you use a .srt file, let me tell you that it’s way better to work with .ass files.
But, if you still want to go ahead and use .srt files, then you can do it like this:

ffmpeg -i video.mp4 -vf subtitles=utf8.srt subtitled-video.mp4

Links