<p>A quick, maybe imperfect solution – use an expression in the <code>volume</code> filter, e.g. for linearly decreasing volume from 1 to 0.15 in the time interval between 4<sup>th</sup> and 5<sup>th</sup> second:</p>
<pre><code>volume=1-0.85/(5-4)*(t-4):eval=frame
</code></pre>
<p>For time  t=4, the volume will evaluate to 1, for t=5 it will be 0.15:</p>
<pre><code>for t=4:    1 - 0.85 / (5-4) * (4-4) = 1 - 0.85 / (10-4) * 0 = 1 - 0 = 1
for t=5:    1 - 0.85 / (5-4) * (5-4) = 1 - 0.85 = 0.15 
</code></pre>
<p><a href="https://i.sstatic.net/18tZ8.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/18tZ8.jpg" alt="enter image description here" /></a></p>
<hr />
<p>You may use (nested) <code>if</code> functions for evaluating the volume value in different time intervals of your audio stream.</p>
<p>For example, to keep the value 1 for the first 4 seconds you may extend the previous formula</p>
<pre><code>              1-0.85/(5-4)*(t-4)
</code></pre>
<p>with the help of the <code>lt</code> (less than) function into</p>
<pre><code>if(lt(t\,4),1,1-0.85/(5-4)*(t-4))
</code></pre>
<p><a href="https://i.sstatic.net/1cLtT.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/1cLtT.jpg" alt="enter image description here" /></a></p>
<p>and then – for keeping the value 0.15 after 5<sup>th</sup> second – into</p>
<pre><code>if(lt(t\,4),1,if(lt(t\,5),1-0.85/(5-4)*(t-4),0.15))
</code></pre>
<p><a href="https://i.sstatic.net/Oq4Pk.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Oq4Pk.jpg" alt="enter image description here" /></a></p>
<p>And so on, always rewriting the last item with a new <code>if</code> function.</p>
<p>Of course, the resulting expression is not the optimal one, but the procedure is pretty straightforward.</p>

CC BY-SA 4.0
2023-06-02T07:37:54.873Z
Hi. Thank you for your answer @MarianD.
From your question i came up with this function to decrease the volume : `1 - [ (1 - percentage) / ( stop - start) ] * ( time - start)`

And this is for the linearly increasing the volume : `1 - [ (1 - percentage) / (stop - start) ] * (stop - time)`

My question is now : I want the volume to decrease to 15% over 1s then stay at the same level between 2 times and finally increase back up to 100% over 1s. How would i go about doing that ?
Nobunaji
CC BY-SA 4.0
2023-06-02T22:44:40.503Z
@Nobunaji, I extended my answer to show you how to progressively build the expression for your particular need.
MarianD
CC BY-SA 4.0
2023-06-03T04:17:03.877Z
finally: `ffmpeg -f lavfi -i "aevalsrc=sin(440*2*PI*t)" -af "
volume='if(lt(t,4)+between(t,10,14)+gt(t,20),1,
if(between(t,4,5),1-0.85*(t-4),
if(between(t,9,10),0.15+0.85*(t-9),
if(between(t,14,15),1-0.85*(t-14),
if(between(t,19,20),0.15+0.85*(t-19),
0.15)))))':eval=frame
" -b:a 320k -t 23 -y /tmp/out.mp3`
Баяр Гончикжапов
CC BY-SA 4.0
2023-06-03T14:34:56.990Z
@БаярГончикжапов, nice, you are a smart man – why not to put it as another answer and gain some reputation points?
MarianD
CC BY-SA 4.0
2023-06-05T08:08:15.627Z
@MarianD Why in your expression you put `if(lt(t\,4),1,if(lt(t\,5),1-0.85/(5-4)*(t-4),0.15))` and not `if(lt(t, 4), 1), if(lt(t, 5), 1-0.85/(5-4)*(t-4),0.15))` ? This makes more sense to me than to have one if inside of another.
Nobunaji
CC BY-SA 4.0
2023-06-05T09:30:13.210Z
@БаярГончикжапов Can any of you two tell me what is wrong with the edited section of my original question ?
Nobunaji
CC BY-SA 4.0
2023-06-05T13:04:16.393Z
Nobunaji, **1.** Yes, your expression seems better than mine. — **2.** Please consider accepting my answer, if it is useful for you (click on the check mark near its start). — **3.** Your edited part is another question, please let us at first finish the original one. Put the edited part in the form of another, new question, please.
MarianD
CC BY-SA 4.0
2023-06-05T13:09:26.457Z
It's done @MarianD. Here is a link to the new question : https://video.stackexchange.com/questions/36556/error-initializing-complex-filters-using-volume-filter-in-fluent-ffmpeg
Nobunaji
CC BY-SA 4.0
2023-06-01T18:48:09.150Z
2023-06-04T18:27:16.203Z
2023-06-04T18:27:16.203Z
MarianD
<p>Here is my current command :</p>
<pre><code>ffmpeg -i [videoFilepath] -i [musicFile] -stream_loop -1 -shortest -filter_complex &quot;[1]volume=0.15:enable=between(t\,4\,10)+between(t\,14\,20)[a1];[0:a][a1]amix=inputs=2&quot; -map 0:v:0 [outputFilepath]
</code></pre>
<p>What it does :</p>
<ul>
<li>Takes a video file as an input</li>
<li>Takes a music file as an input</li>
<li>Loops the music if it is shorter than the video duration</li>
<li>If the music is longer, outputs a file the duration of the video</li>
<li>Reduce volume by 85% between 4 and 10 seconds as well as between 14 and 20 seconds</li>
<li>Mixes the 2 audio streams so we have both the original video audio non-filtered and the music with the volume filter</li>
<li>Outputs a video file</li>
</ul>
<p>I want the volume filter to ease-in and ease-out for a duration of 1 second, instead of abruptly changing. Is this possible ?</p>

<ffmpeg>
How to add a afade effect to a complex filter (In this case volume)?
Nobunaji
# How to add a afade effect to a complex filter (In this case volume)?

(Asked by: username_0 on 2023-06-01)

Here is my current command :
ffmpeg -i [videoFilepath] -i [musicFile] -stream_loop -1 -shortest -filter_complex "[1]volume=0.15:enable=between(t\,4\,10)+between(t\,14\,20)[a1];[0:a][a1]amix=inputs=2" -map 0:v:0 [outputFilepath]

What it does :

Takes a video file as an input
Takes a music file as an input
Loops the music if it is shorter than the video duration
If the music is longer, outputs a file the duration of the video
Reduce volume by 85% between 4 and 10 seconds as well as between 14 and 20 seconds
Mixes the 2 audio streams so we have both the original video audio non-filtered and the music with the volume filter
Outputs a video file

I want the volume filter to ease-in and ease-out for a duration of 1 second, instead of abruptly changing. Is this possible ?

**Tags:** ffmpeg


---

## 1 answers

### Answer by username_1 on 2023-06-01. Score: 0 | Accepted answer

A quick, maybe imperfect solution – use an expression in the volume filter, e.g. for linearly decreasing volume from 1 to 0.15 in the time interval between 4th and 5th second:
volume=1-0.85/(5-4)*(t-4):eval=frame

For time  t=4, the volume will evaluate to 1, for t=5 it will be 0.15:
for t=4:    1 - 0.85 / (5-4) * (4-4) = 1 - 0.85 / (10-4) * 0 = 1 - 0 = 1
for t=5:    1 - 0.85 / (5-4) * (5-4) = 1 - 0.85 = 0.15 



You may use (nested) if functions for evaluating the volume value in different time intervals of your audio stream.
For example, to keep the value 1 for the first 4 seconds you may extend the previous formula
              1-0.85/(5-4)*(t-4)

with the help of the lt (less than) function into
if(lt(t\,4),1,1-0.85/(5-4)*(t-4))


and then – for keeping the value 0.15 after 5th second – into
if(lt(t\,4),1,if(lt(t\,5),1-0.85/(5-4)*(t-4),0.15))


And so on, always rewriting the last item with a new if function.
Of course, the resulting expression is not the optimal one, but the procedure is pretty straightforward.

> **Comments:**
> **username_0**: Hi. Thank you for your answer @username_1. From your question i came up with this function to decrease the volume : `1 - [ (1 - percentage) / ( stop - start) ] * ( time - start)`  And this is for the linearly increasing the volume : `1 - [ (1 - percentage) / (stop - start) ] * (stop - time)`  My question is now : I want the volume to decrease to 15% over 1s then stay at the same level between 2 times and finally increase back up to 100% over 1s. How would i go about doing that ?  
> **username_1**: @username_0, I extended my answer to show you how to progressively build the expression for your particular need.  
> **username_2**: finally: `ffmpeg -f lavfi -i "aevalsrc=sin(440*2*PI*t)" -af " volume='if(lt(t,4)+between(t,10,14)+gt(t,20),1, if(between(t,4,5),1-0.85*(t-4), if(between(t,9,10),0.15+0.85*(t-9), if(between(t,14,15),1-0.85*(t-14), if(between(t,19,20),0.15+0.85*(t-19), 0.15)))))':eval=frame " -b:a 320k -t 23 -y /tmp/out.mp3`  
> **username_1**: @БаярГончикжапов, nice, you are a smart man – why not to put it as another answer and gain some reputation points?  
> **username_0**: @username_1 Why in your expression you put `if(lt(t\,4),1,if(lt(t\,5),1-0.85/(5-4)*(t-4),0.15))` and not `if(lt(t, 4), 1), if(lt(t, 5), 1-0.85/(5-4)*(t-4),0.15))` ? This makes more sense to me than to have one if inside of another.  
> **username_0**: @БаярГончикжапов Can any of you two tell me what is wrong with the edited section of my original question ?  
> **username_1**: username_0, **1.** Yes, your expression seems better than mine. — **2.** Please consider accepting my answer, if it is useful for you (click on the check mark near its start). — **3.** Your edited part is another question, please let us at first finish the original one. Put the edited part in the form of another, new question, please.  
> **username_0**: It's done @username_1. Here is a link to the new question : https://video.stackexchange.com/questions/36556/error-initializing-complex-filters-using-volume-filter-in-fluent-ffmpeg  

---

