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 "format=rgba,scale=300:250[o];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
ffmpeg -i background.jpeg -framerate 30 -loop 1 -i logo.png -filter_complex "scale=300:250[o];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?