How to set subtitle background in ffmpeg?

4 answers

Using ASS subtitles to create opaque text background

ASS subtitles can have a translucent background for text.

Using aegisub

The easiest way to do this is aegisub .

enter image description here

  • Open the subtitle file with eegisub.
  • Click SubtitleStyles manager .
  • In Current Script select Default , then click the Edit button.
  • Experiment with Outline and Shadow values. Check out the Opaque box .
  • Under Colors click a color under Outline or Shadows . A window will appear. Adjust the value of the Alpha field to change the transparency.
  • Save the subtitles as an .ass file.

Now you can use the AAS file to create hardsubs or softsubs with ffmpeg .

Without aegisub

If you want hardsubs, you can use the subtitle filter to add a 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 subtitles supported by FFmpeg, because the filter will automatically convert them to ASS.

  • For formatting, see SubStation Alpha (ASS) style fields .

Multiple Line Problem

If your subtitles contain multiple lines, due to the automatic tying of long lines or an intentional line break, the backgrounds will overlap and potentially look ugly, as shown below:

enter image description here

You can avoid this:

  • Resize Outline and Shadow to 0 .
  • Alpha shadow settings will control the transparency of the background. Click on the shadow color to adjust the Alpha shadow color to the desired level of transparency.
  • Edit the ASS file in a text editor. In the Style line, change the value corresponding to BorderStyle to 4 . This will fill the frame background for each subtitle event. Style line example:

     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:

enter image description here

Please note that BorderStyle=4 is a non-standard value, so it may not work properly for all players.

Thanks to sup and wm4 for the BorderStyle suggestion.

Using drawbox

The drawbox filter can be used to create the background. This can be useful if you want the field to span the width.

enter image description here

 ffmpeg -i input -filter_complex "drawbox=w=iw:h=24:y=ih-28:t=max: color=black@0.4 ,subtitles=subs.ass" output 

Downside is something that you have to consider line breaks or word wrap for long subtitles. Just making the height of the drawer to compensate will be enough, but it will look ugly, because the basic level of subtitles remains static: in single-line subtitles there will be more indents from above than below.

+10
source share

Create a png with a transparent box and alpha in your preferred size. You can use for example. gimp or photoshop.

Then use the following command:

 ffmpeg -i video.mp4 -i logo.png -filter_complex "[0:v][1:v]overlay=10:10" \ -codec:a copy out.mp4 

where 10:10 is the distance from the upper left corner.

After that you can insert your subtitles.

+4
source share

You can use this Aegisub script. This script automatically generates a transparent background for each line of subtitles.

Subtitles transparent background

0
source share

Had the same question, no good answer was found. Apparently ffmpeg (and avconv can apply ass formatting to srt like this:

 avconv -i in.mp4 -vf "subtitles=subtemp.sub.0.sub:force_style='Name=Default,Fontname=Arial,Fontsize=28,PrimaryColour=&Hffffff,SecondaryColour=&Hffffff,OutlineColour=&H44000000,BackColour=&H0,BorderStyle=3,Shadow=0'" out.mp4 

(replacing avconv with ffmpeg should work or close to it).

Further reading of ass , in particular, what matters is BackColour and BorderStyle .

-one
source share

All Articles