ankushalg
ankushalg

Reputation: 553

FFmpeg : How to create dynamic volume changes in an audio file?

I am working on a project in which I need to change the volume of an audio file dynamically.

Let's say, I have an audio file with name xyz.mp3 (20 - seconds audio file). I need to set the volume in it like :

Time Range (in Seconds)     ||     Volume Percentage (in %)
-------------------------------------------------------------------    
    0 - 4                   ||           100
                            ||
    4 - 8                   ||    change from 100 - 20 (dynamically)
                            ||
    8 - 12                  ||           20
                            ||           
    12 - 16                 ||    change from 20 - 100 (dynalically)
                            ||
    16 - 20                 ||           100       

Now, I know that to change the volume for a particular time in audio, I can use the following command :

ffmpeg -i in.mp3 -af volume=20:enable='between(t,8,12)' out.mp3

but when I use volume effect, it does not change the volume dynamically. It just straightly change volume from 100 to 20 and not change it like fading.

and When I use afade command like :

ffmpeg -i in.mp3 -af afade=t=in:ss=4:d=8,afade=t=out:st=12:d=16 out.mp3

or

ffmpeg -i in.mp3 -af afade=enable='between(t,4,8)':t=in:ss=4:d=4,afade=enable='between(t,12,16)':t=out:st=12:d=4 out.mp3

but it looks that afade does not work multiple times even when I am using ffmpeg 3.0.1 version.

As afade only works single time, I had also split my audio into parts of 4 second and add fade effects to it and then combine them again, but there is some milliseconds gap comes between each clip. Does anyone know a better way to do it? Please help me...

Update 1 :

Here is that code I used :

"volume='" +
            "between(t,0,8)+(1-0.8*(t-8)/4)*" +        // full
            "between(t,8.01,11.99)+0.1*" +             // change from HIGH -> LOW
            "between(t,12,16)+(0.1+0.8*(t-16)/4)*" +   // low
            "between(t,16.01,19.99)+1*" +              // change from LOW -> HIGH   -
            "between(t,20,24)+(1-0.8*(t-24)/4)*" +     // full
            "between(t,24.01,27.99)+0.1*"+             // change from HIGH -> LOW   -
            "between(t,28,32)+(0.1+0.8*(t-32)/4)*" +   // low
            "between(t,32.01,35.99)+1*" +              // change from LOW -> HIGH  -
            "between(t,36,40)+(1-0.8*(t-40)/4)*" +     // full
            "between(t,40.01,43.99)+0.1*"+             // change from HIGH -> LOW   -
            "between(t,44,48)+(0.1+0.8*(t-48)/4)*" +   // low
            "between(t,48.01,51.99)+" +                // change from LOW -> HIGH -
            "between(t,52,56)" +                       // high
            "':eval=frame";

In this code, I got a small (some milliseconds gap) at those places where I initialize the audio to change the volume

Update 2 Ok I got it, I just need to change the time values like 19.99 to 19.9999 and 16.01 to 16.0001 and it solve the problem. Thank You Gyaan Sir.

Upvotes: 5

Views: 1501

Answers (1)

Gyan
Gyan

Reputation: 92938

Use

volume='between(t,0,4)+(1-0.8*(t-4)/4)*between(t,4.01,7.99)+0.2*between(t,8,12)+(0.2+0.8*(t-12)/4)*between(t,12.01,15.99)+between(t,16,20)':eval=frame

Upvotes: 5

Related Questions