乐闻世界logo
搜索文章和话题

How to set background to subtitle in ffmpeg?

1个答案

1

When processing video files with FFmpeg, you may need to add subtitles and set their background to improve readability. Setting the subtitle background can be achieved using FFmpeg's filter capabilities. Below is a specific example demonstrating how to set a simple background for subtitles:

Step 1: Prepare the Subtitle File

First, ensure you have a subtitle file, typically in .srt format. For example, you have a subtitle file named subtitles.srt.

Step 2: Add Subtitle Background Using FFmpeg

Use FFmpeg's ass filter to add subtitles and the drawbox filter to draw the background. Here is an example using the FFmpeg command line:

bash
ffmpeg -i input.mp4 -vf "subtitles=subtitles.srt:force_style='FontName=Arial,FontSize=24,PrimaryColour=&Hffffff&,OutlineColour=&H000000&,BorderStyle=3,BackColour=&H80000000&,Bold=2',drawbox=y=ih/PHI:color=black@0.4:width=iw:height=48:t=max" -codec:a copy output.mp4

Explanation

  1. Input File -i input.mp4: This specifies the video file to which you want to add subtitles.

  2. Subtitle Filter subtitles=subtitles.srt: This indicates the subtitle file FFmpeg should use.

  3. force_style: This option customizes the subtitle style. For instance, FontName=Arial sets the font, FontSize=24 sets the font size, PrimaryColour=&Hffffff& sets the font color (white), OutlineColour=&H000000& sets the outline color (black), and BackColour=&H80000000& sets the background color (semi-transparent black).

  4. drawbox Filter: drawbox=y=ih/PHI:color=black@0.4:width=iw:height=48:t=max This filter draws a background box in the subtitle area. y=ih/PHI sets the vertical position, color=black@0.4 sets the color and transparency, and width=iw and height=48 set the width and height of the box.

  5. Output File -codec:a copy output.mp4: This specifies the output file name and copies the audio codec.

By implementing this approach, you can effectively add subtitles to the video and provide a background to enhance both visibility and aesthetics.

2024年8月15日 02:30 回复

你的答案