<p>geq, overlay</p>
<pre><code>ffmpeg -i input.mp4 -lavfi &quot;
color=black:size=100x100,geq=r=0:g=0:b='lerp(255,127,Y/H)'[o];
[0:v][o]overlay=100:100:shortest=1
&quot; -y out.mp4
</code></pre>
<p>drawtext, geq, overlay</p>
<pre><code>ffmpeg -i &quot;in 1.mp4&quot; -lavfi &quot;
color=black@0:size=1280x720,
drawtext=text='Some text'
:box=1
:boxborderw=30
:boxcolor=blue
:borderw=5
:bordercolor=red
:fontsize=h/4
:fontcolor=white
:x=(w-text_w)/2
:y=(h-text_h)/2,
format=rgba,geq=
r='if(eq(r(X,Y),0)*eq(g(X,Y),0)*eq(b(X,Y),255),0,r(X,Y))'
:g='if(eq(r(X,Y),0)*eq(g(X,Y),0)*eq(b(X,Y),255),0,g(X,Y))'
:b='if(eq(r(X,Y),0)*eq(g(X,Y),0)*eq(b(X,Y),255),lerp(63,255,Y/H),b(X,Y))'
:a='if(eq(r(X,Y),0)*eq(g(X,Y),0)*eq(b(X,Y),0),0,255)'[t];
[0][t]overlay=(W-w)/2:(H-h)/2
&quot; -c:a copy out.mp4 -y
</code></pre>

CC BY-SA 4.0
2023-04-17T07:07:14.617Z
Thank you! It works!
Keisuke Toyoda
CC BY-SA 4.0
2023-04-20T08:28:42.980Z
Actually, I want to use the box above as a background for the text. Is it possible to resize the box to fit the width of the text?
Keisuke Toyoda
CC BY-SA 4.0
2023-04-20T10:03:23.313Z
added drawtext. not tested as well
Баяр Гончикжапов
CC BY-SA 4.0
2023-04-20T10:41:40.040Z
Thank you very much! You have just made it possible for me to achieve exactly what I wanted to do.
Keisuke Toyoda
CC BY-SA 4.0
2023-04-16T08:54:10.733Z
2023-04-20T10:00:31.840Z
2023-04-20T10:00:31.840Z
Баяр Гончикжапов
<p>I tried to run the following code, but it didn't work.</p>
<pre><code>ffmpeg -i input.mp4 -filter_complex [0:v]drawbox=x=100:y=100:w=100:h=100:color='r=66-ceil(48*Y/H):g=74-ceil(59*Y/H):b=199-ceil(72*Y/H)':t=fill -c:a copy output.mp4
</code></pre>
<p>Please teach me how to overlay a rectangular shape with a vertical gradient onto a video, with a constant duration.<br />
I mean that I want to place an image similar to the one shown below over a video.<br />
<a href="https://i.sstatic.net/xY1xT.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/xY1xT.png" alt="enter image description here" /></a></p>

<ffmpeg>
How to dynamically set the color of a box using rgba in 'drawbox' in ffmpeg?
Keisuke Toyoda
# How to dynamically set the color of a box using rgba in 'drawbox' in ffmpeg?

(Asked by: username_0 on 2023-04-15)

I tried to run the following code, but it didn't work.
ffmpeg -i input.mp4 -filter_complex [0:v]drawbox=x=100:y=100:w=100:h=100:color='r=66-ceil(48*Y/H):g=74-ceil(59*Y/H):b=199-ceil(72*Y/H)':t=fill -c:a copy output.mp4

Please teach me how to overlay a rectangular shape with a vertical gradient onto a video, with a constant duration.
I mean that I want to place an image similar to the one shown below over a video.

**Tags:** ffmpeg


---

## 1 answers

### Answer by username_1 on 2023-04-16. Score: 1 | Accepted answer

geq, overlay
ffmpeg -i input.mp4 -lavfi "
color=black:size=100x100,geq=r=0:g=0:b='lerp(255,127,Y/H)'[o];
[0:v][o]overlay=100:100:shortest=1
" -y out.mp4

drawtext, geq, overlay
ffmpeg -i "in 1.mp4" -lavfi "
color=black@0:size=1280x720,
drawtext=text='Some text'
:box=1
:boxborderw=30
:boxcolor=blue
:borderw=5
:bordercolor=red
:fontsize=h/4
:fontcolor=white
:x=(w-text_w)/2
:y=(h-text_h)/2,
format=rgba,geq=
r='if(eq(r(X,Y),0)*eq(g(X,Y),0)*eq(b(X,Y),255),0,r(X,Y))'
:g='if(eq(r(X,Y),0)*eq(g(X,Y),0)*eq(b(X,Y),255),0,g(X,Y))'
:b='if(eq(r(X,Y),0)*eq(g(X,Y),0)*eq(b(X,Y),255),lerp(63,255,Y/H),b(X,Y))'
:a='if(eq(r(X,Y),0)*eq(g(X,Y),0)*eq(b(X,Y),0),0,255)'[t];
[0][t]overlay=(W-w)/2:(H-h)/2
" -c:a copy out.mp4 -y

> **Comments:**
> **username_0**: Thank you! It works!  
> **username_0**: Actually, I want to use the box above as a background for the text. Is it possible to resize the box to fit the width of the text?  
> **username_1**: added drawtext. not tested as well  
> **username_0**: Thank you very much! You have just made it possible for me to achieve exactly what I wanted to do.  

---

