You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何利用FFMPEG库实现从指定颜色到透明色的渐变色彩效果

Fixing Gradient-to-Transparent Text Box in FFmpeg

Got it, let's get that smooth fade-from-color-to-transparent effect working for your text box! Your current command has a couple of issues: you’re setting boxcolor twice without any gradient logic, and the enable clause is incomplete (it needs a valid end time instead of between(t,14,t)). Here’s a working solution tailored to your needs:

Working Command

ffmpeg -i input.mp4 -vf "drawtext=fontfile=Alaska.ttf:text='demo text':fontcolor=black:fontsize=30:box=1:boxborderw=5:x=1:y=550:enable='between(t,14,17)':boxcolor='0x483D8B%{eif:255*(1-(t-14)/3):d}'" -codec:a copy output.mp4

Breakdown of Key Changes

Let’s walk through the parts that make the gradient happen:

  • enable='between(t,14,17)': This restricts the text box to appear only between 14 second and 17 second (a 3-second window for the fade). Adjust the end time (17) to make the gradient shorter or longer.
  • boxcolor='0x483D8B%{eif:255*(1-(t-14)/3):d}': This is the magic for the transparent fade:
    • 0x483D8B is your base solid color (dark slate blue).
    • The %{eif:...} expression dynamically calculates the alpha channel (transparency) over time:
      • (t-14) calculates how many seconds have passed since the gradient started.
      • (t-14)/3 converts that time to a 0-to-1 ratio (where 1 is the end of the 3-second fade).
      • 1-(t-14)/3 reverses the ratio so alpha starts at 1 (fully opaque) and drops to 0 (fully transparent).
      • 255*... scales the 0-to-1 value to the 0-to-255 range FFmpeg uses for alpha.
      • eif:...:d converts the calculated value to a decimal integer, which gets appended to the base color to form the full 0xRRGGBBAA color format.

Customization Options

  • Reverse the Gradient: If you want to fade from transparent to solid color instead, flip the alpha expression:
    boxcolor='0x483D8B%{eif:255*((t-14)/3):d}'
    
  • Fade the Text Too: To make the text itself fade along with the box, apply the same alpha logic to fontcolor:
    fontcolor='black%{eif:255*(1-(t-14)/3):d}'
    
  • Adjust Fade Duration: Change the 3 in the expression to any number of seconds you want the fade to last (just make sure the end time in enable matches: e.g., 14 + duration).

Quick Note

Make sure you’re using a relatively recent version of FFmpeg—older versions might not support dynamic expressions in boxcolor or fontcolor.

内容的提问来源于stack exchange,提问作者Samir Shaikh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 06:50:37