如何利用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:0x483D8Bis 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)/3converts that time to a 0-to-1 ratio (where 1 is the end of the 3-second fade).1-(t-14)/3reverses 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:...:dconverts the calculated value to a decimal integer, which gets appended to the base color to form the full0xRRGGBBAAcolor 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
3in the expression to any number of seconds you want the fade to last (just make sure the end time inenablematches: 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
相关产品推荐
相关产品推荐

