I'm having difficulty achieving the desired fading effect for the logo in my video using ffmpeg.
I would like to make a 3-second video (here is an example) with:
- A background image (300x350 jpeg file)
- A logo overlaid in the center of the background image (100x40 png file)
- And I would like the logo to fade in and out with the following opacity timeline:
- At 0 second, the opacity is 0.5
- At 1 second, the opacity is 1
- At 2 second, the opacity is 0.2
- At 3 second, the opacity is 1.
As the fade filter does not support any target or source value (it is a full fade from 0 to 1 or the reverse), I cannot use it.
For example: Here is how to add an overlay with a fully fade IN (between 0 to 1s) and a fully fade OUT (from 2s to 3s):
ffmpeg -loop 1 -i background.jpeg -framerate 30 -loop 1 -i logo.png -filter_complex "[0]format=rgba,scale=300:250[o];[1]format=rgba,fade=in:st=0:d=1:alpha=1,fade=out:st=2:d=1:alpha=1[i];[o][i]overlay=x=(W-w)/2:(H-h)/2" -c:v libx264 -t 3 output.mp4
I also tried the geq
filter with a lerp
operation but it is quite difficult to write (and the result I got is wrong, I may have a problem with the lerp
operator):
ffmpeg -i background.jpeg -framerate 30 -loop 1 -i logo.png -filter_complex "[0]scale=300:250[o];[1]format=yuva420p,geq=a='if(lt(T\,1)\,lerp(0\,1\,T)*0.5*alpha(X\,Y)\,if(lt(T\,2)\,lerp(1\,2\,T)*0.2*-alpha(X\,Y),lerp(2\,3\,T)*alpha(X\,Y)))':r='r(X\,Y)'[i];[o][i]overlay=x=(W-w)/2:(H-h)/2" -c:v libx264 -t 3 -pix_fmt yuv420p -crf 0 output.mp4
I also found that scripting filters (https://trac.ffmpeg.org/wiki/FilteringGuide#ScriptingFilters) are also available for the geq
filter but I haven't found a way to use it for that specific use case.
What would be the more appropriate approach to handle that kind of effect?
geq=a='if(lt(T,1),lerp(0.5,1,T)*alpha(X,Y),if(lt(T,2),lerp(1,0.2,T-1)*alpha(X,Y),lerp(0.2,1,T-2)*alpha(X,Y)))':r='r(X,Y)'
...[1]format=gbrap, split=3[s1][s2][s3]; [s1]geq=a='lerp(0.5,1,T)*alpha(X,Y)':r='r(X,Y)'[v1]; [s2]geq=a='lerp(1,0.2,T)*alpha(X,Y)':r='r(X,Y)'[v2]; [s3]geq=a='lerp(0.2,1,T)*alpha(X,Y)':r='r(X,Y)'[v3]; [v1][v2][v3]concat=n=3:v=1:a=0[i];...
and insert-t 1
before logo.png