It is described here how ot burn a srt file into a video. However, I want to put a semi-transparent background to the subtitles so that the texts can be read more easily. How can I do that?
FFmpeg uses the subtitles filter and the libass ↗ library to add SRT subtitles on top of the input video. The libass library is a portable subtitle renderer for the ASS subtitle format. It converts the input subtitle file to an ASS file. To use the libass library, you need to have libass enabled at compile time.
Just like when you extract subtitles from DVD, converting PGS to SRT, OCR (Optical Character Recognition) is a necessary process. Simply put, you need a third-party tool to identify the subtitles on the image and then transcribe it to text-based SRT subtitle files.

ASS subtitles can have a semi-transparent background for the text.
The easiest way to do this is with aegisub.

Subtitle → Styles manager.Current Script choose Default, then press the Edit button.Outline and Shadow values. Check Opaque box.Colors click the color under Outline or Shadows. A window will appear. Adjust the value of the Alpha box to change transparency..ass file.Now you can use the AAS file to make hardsubs or softsubs with ffmpeg.
If you want hardsubs you can use the subtitles filter to add the transparent background with the force_style option.
ffmpeg -i input -filter_complex "subtitles=subs.ass:force_style='OutlineColour=&H80000000,BorderStyle=3,Outline=1,Shadow=0,MarginV=20'" output
This will work with any text based subtitles supported by FFmpeg because the filter will automatically convert them to ASS.
See SubStation Alpha (ASS) style fields for formatting options.
If your subtitles contains multiple lines, due to auto-wrapping of long lines or an intentional line break, the backgrounds will overlap and potentially look ugly as shown below:

You can avoid this by:
Outline and Shadow sizes to 0.Alpha of the shadow color to your desired transparency level.Edit the ASS file in a text editor. In the Style line change the value corresponding with BorderStyle to 4. This will fill the bounding box background of each subtitle event. Example Style line:
Style: Default,Arial,20,&H00FFFFFF,&H000000FF,&H80000000,&H80000000,-1,0,0,0,100,100,0,0,4,0,0,2,10,10,10,1
Example:

Note that BorderStyle=4 is a non-standard value, so it may not work properly in all players.
Thanks to sup and wm4 for the BorderStyle suggestion.
The drawbox filter can be used to create a background box. This may be useful if you want the box to span the width.

ffmpeg -i input -filter_complex "drawbox=w=iw:h=24:y=ih-28:t=max:[email protected],subtitles=subs.ass" output
Downside is that you have to account for line breaks or word wrapping for long subtitles. Simply making the box taller to compensate will suffice, but will look ugly because the subtitles baseline remains static: the single line subtitles will have more padding on the top than the bottom.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With