Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-02T07:37:54.873Z
Answers.Text: 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 ?
Answers.UserDisplayName: Nobunaji
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-02T22:44:40.503Z
Answers.Text: @Nobunaji, I extended my answer to show you how to progressively build the expression for your particular need.
Answers.UserDisplayName: MarianD
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-03T04:17:03.877Z
Answers.Text: 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`
Answers.UserDisplayName: Баяр Гончикжапов
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-03T14:34:56.990Z
Answers.Text: @БаярГончикжапов, nice, you are a smart man – why not to put it as another answer and gain some reputation points?
Answers.UserDisplayName: MarianD
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-05T08:08:15.627Z
Answers.Text: @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.
Answers.UserDisplayName: Nobunaji
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-05T09:30:13.210Z
Answers.Text: @БаярГончикжапов Can any of you two tell me what is wrong with the edited section of my original question ?
Answers.UserDisplayName: Nobunaji
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-05T13:04:16.393Z
Answers.Text: 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.
Answers.UserDisplayName: MarianD
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-05T13:09:26.457Z
Answers.Text: 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
Answers.UserDisplayName: Nobunaji
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-01T18:48:09.150Z
Answers.LastActivityDate: 2023-06-04T18:27:16.203Z
Answers.LastEditDate: 2023-06-04T18:27:16.203Z
Answers.UserDisplayName: MarianD
Body: <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>
  
Tags: <ffmpeg>
Title: How to add a afade effect to a complex filter (In this case volume)?
UserDisplayName: Nobunaji
ThreadText: # 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 
  
  ```
  
  [![enter image description here](https://i.sstatic.net/18tZ8.jpg)](https://i.sstatic.net/18tZ8.jpg)
  
  ---
  
  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))
  
  ```
  
  [![enter image description here](https://i.sstatic.net/1cLtT.jpg)](https://i.sstatic.net/1cLtT.jpg)
  
  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))
  
  ```
  
  [![enter image description here](https://i.sstatic.net/Oq4Pk.jpg)](https://i.sstatic.net/Oq4Pk.jpg)
  
  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**: @username_2, 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**: @username_2 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  
  
  ---
  
  