Body: <p>I'm having difficulty achieving the desired fading effect for the logo in my video using ffmpeg.</p>
  <p>I would like to make a 3-second video (<a href="https://local-banners.s3.eu-west-1.amazonaws.com/video/video-with-partial-fade-effects.mp4" rel="nofollow noreferrer">here is an example</a>) with:</p>
  <ul>
  <li>A background image (300x350 jpeg file)</li>
  <li>A logo overlaid in the center of the background image (100x40 png file)</li>
  <li>And I would like the logo to fade in and out with the following opacity timeline:</li>
  </ul>
  <ol>
  <li>At 0 second, the opacity is 0.5</li>
  <li>At 1 second, the opacity is 1</li>
  <li>At 2 second, the opacity is 0.2</li>
  <li>At 3 second, the opacity is 1.</li>
  </ol>
  <p>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.</p>
  <p><em>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):</em></p>
  <pre><code>ffmpeg -loop 1 -i background.jpeg -framerate 30 -loop 1 -i logo.png -filter_complex &quot;[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&quot; -c:v libx264 -t 3 output.mp4
  </code></pre>
  <p>I also tried the <code>geq</code> filter with a <code>lerp</code> operation but it is quite difficult to write (and the result I got is wrong, I may have a problem with the <code>lerp</code> operator):</p>
  <pre><code>ffmpeg -i background.jpeg -framerate 30 -loop 1 -i logo.png -filter_complex &quot;[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&quot; -c:v libx264 -t 3 -pix_fmt yuv420p -crf 0 output.mp4
  </code></pre>
  <p>I also found that scripting filters (<a href="https://trac.ffmpeg.org/wiki/FilteringGuide#ScriptingFilters" rel="nofollow noreferrer">https://trac.ffmpeg.org/wiki/FilteringGuide#ScriptingFilters</a>) are also available for the <code>geq</code> filter but I haven't found a way to use it for that specific use case.</p>
  <p>What would be the more appropriate approach to handle that kind of effect?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-10-24T03:57:11.397Z
Comments.Text: try `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)'`
Comments.UserDisplayName: Баяр Гончикжапов
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-10-25T10:51:48.850Z
Comments.Text: Thank you, that works. However, do you know of a simpler way to achieve this effect? Maybe the scripting filter way?
Comments.UserDisplayName: antoox
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-10-26T03:54:17.157Z
Comments.Text: u can try: `...[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
Comments.UserDisplayName: Баяр Гончикжапов
Tags: <ffmpeg>
Title: FFMPEG - Partial fade effects
UserDisplayName: antoox
ThreadText: # FFMPEG - Partial fade effects
  
  (Asked by: username_0 on 2023-10-23)
  
  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](https://local-banners.s3.eu-west-1.amazonaws.com/video/video-with-partial-fade-effects.mp4)) 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:
  
  1. At 0 second, the opacity is 0.5
  2. At 1 second, the opacity is 1
  3. At 2 second, the opacity is 0.2
  4. 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?
  
  **Tags:** ffmpeg
  
  > **Comments:**
  > **username_1**: try `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)'`  
  > **username_0**: Thank you, that works. However, do you know of a simpler way to achieve this effect? Maybe the scripting filter way?  
  > **username_1**: u can try: `...[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  
  
  ---
  
  ## 0 answers
  
  