Skip to content

Add subtitles to a video in Linux

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

2 min read

.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:

Shell command
file some.srt

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

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

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

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

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

Shell command
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:

Shell command
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.

Shell command
nano subs.ass

Hardcode the subtitles

Save and exit, then hardcode the subtitles with:

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

That's it! 🎉

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:

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

More posts connected by shared tags.