FFMPEG amix filter volume issue with inputs of dif

2019-01-26 21:11发布

I noticed that ffmpeg amix filter doesn't output good result in specific situation. It works fine if input files have equal duration. In that case volume is dropped in constant value and could be fixed with ",volume=2".

In my case I'm using files with different duration. Resulted volume is not good. First mixed stream resulted in lowest volume, and last one is highest. You can see on image that volume is increased linearly withing a time.

enter image description here

My command:

ffmpeg -i temp_0.mp4 -i user_2123_10.mp4  -i user_2123_3.mp4  -i user_2123_4.mp4  
-i user_2123_7.mp4  -i user_2123_5.mp4  -i user_2123_1.mp4  -i user_2123_8.mp4  
-i user_2123_0.mp4  -i user_2123_6.mp4  -i user_2123_9.mp4  -i user_2123_2.mp4  
-i user_2123_11.mp4 -filter_complex "[1:a]adelay=34741.0[aud1];
[2:a]adelay=18241.0[aud2];[3:a]adelay=20602.0[aud3];
[4:a]adelay=27852.0[aud4];[5:a]adelay=22941.0[aud5];
[6:a]adelay=13142.0[aud6];[7:a]adelay=29810.0[aud7];
[8:a]adelay=12.0[aud8];[9:a]adelay=25692.0[aud9];
[10:a]adelay=32143.002[aud10];[11:a]adelay=16101.0[aud11];
[12:a]adelay=40848.0[aud12];
[0:a][aud1][aud2][aud3][aud4][aud5][aud6][aud7]
[aud8][aud9][aud10][aud11]
[aud12]amix=inputs=13:duration=first:dropout_transition=0" 
-vcodec copy -y temp_1.mp4

That could be fixed by applying silence at the beginning and end of each clip, then they will have same duration and volume will be at the same level.

Please suggest how I can use amix to mix many inputs and ensure constant volume level.

标签: audio ffmpeg mix
8条回答
仙女界的扛把子
2楼-- · 2019-01-26 21:20

try to change dropout transition to the duration of the first input:

duration=first:dropout_transition=_duration_of_the_first_input_in_seconds_

here is my ffmpeg command:

ffmpeg -y -i long.wav -i short.wav  -filter_complex "[1:a]adelay=6000|6000[a1];[1:a]adelay=10000|10000[a2];[1:a]adelay=14000|14000[a3];[1:a]adelay=18000|18000[a4];[1:a]adelay=21000|21000[a5];[1:a]adelay=25500|25500[a6];[0:a][a1][a2][a3][a4][a5][a6]amix=inputs=7:duration=first:dropout_transition=32[aout]" -map "[aout]" -ac 2 -b:a 192k -ar 44100 output.mp3

see two dropout transitions as screenshot

查看更多
时光不老,我们不散
3楼-- · 2019-01-26 21:23

The solution seems to be a combination of "pre-amp", or multiplication, as Maxim puts it, AND you have to set dropout_transition >= max delay + max input length (or a very high number):

amix=inputs=13:dropout_transition=1000,volume=13

Notes:

  • amix has to resample float anyway, so there is no downside with adding the volume filter (which by default resamples to float, too).
    And since we're using floats, there's no clipping and (almost) no loss of precision.
  • H't to @Mulvya for the analysis but their solution is frustratingly non-mathematical
  • I was originally trying to do this with sox, which was too slow. Sox's remix filter has the -m switch which disables the 1/n adjustment.
  • While faster, ffmpeg seems to be using way more memory for the same task. YMMV - I didn't test this thoroughly, because I finally settled on a small python script which uses pydub's overlay function, and only keeps the final output file and one segment in memory (whereas ffmpeg and sox seem to keep all of the segments in memory).
查看更多
女痞
4楼-- · 2019-01-26 21:29

Try to use multiplication:

"amix=inputs="+ chunks.length + ":duration=first:dropout_transition=3,volume=" + chunks.length
查看更多
Animai°情兽
5楼-- · 2019-01-26 21:31

amix scales each input's volume by 1/n where n = no. of active inputs. This is evaluated for each audio frame. So when an input drops out, the volume of the remaining inputs is scaled by a smaller amount, hence their volumes increase.

Changing the dropout_transition for all earlier inputs, as suggested in other answers, is one approach, but I think it will result in coarse volume modulations. Better method is to normalize the audio after the amix.

At present, you have two options, the loudnorm or the dynaudnorm filter. The latter is much faster

Syntax is to add it after the amix, so

[aud11][aud12]amix=inputs=13:duration=first:dropout_transition=0,dynaudnorm"

Read the documentation, if you wish to tweak parameters for maximum volume or RMS mode normalization..etc

查看更多
We Are One
6楼-- · 2019-01-26 21:34

Sorry, for not sending ffmpeg output.

After all we ended up by writing small util in C++ for mixing audio. But first we converted mp4 to raw(pcm) format. That worked just fine for us, even requires addition HDD space for raw intermediate files.

Code looks like this:

short addSounds(short a, short b) {
    double da = a;
    da /= 65536.0;
    da += 0.5;
    double db = b;
    db /= 65536.0;
    db += 0.5;
    double z = 0;
    if (da < 0.5 && db < 0.5) {
        z = 2 * da*db;
    }
    else {
        z = 2 * ( da + db ) - 2 * da* db - 1;
    }
    z -= 0.5;
    z *= 65536.0;
    return (short)z;
}
查看更多
Rolldiameter
7楼-- · 2019-01-26 21:40

The solution I've found is to specify the volume for each track in a "descendant" order and use no normalization filter afterwards.

I use this example, where I concat the same audio file in different positions:

ffmpeg -vn -i test.mp3 -i test.mp3 -i test.mp3 -filter_complex "[0]adelay=0|0,volume=3[a];[1]adelay=2000|2000,volume=2[b];[2]adelay=4000|4000,volume=1[c];[a][b][c]amix=inputs=3:dropout_transition=0" -q:a 1 -acodec libmp3lame -y amix-volume.mp3

More details, see this image. The first track is the normal mixing, the second is the one with volumes specified; the third is the original track. As we can see the 2nd track looks to have a normal volume.

enter image description here

ffmpeg -vn -i test.mp3 -i test.mp3 -i test.mp3 -filter_complex "[0]adelay=0|0[a];[1]adelay=2000|2000[b];[2]adelay=4000|4000[c];[a][b][c]amix=inputs=3:dropout_transition=0" -q:a 1 -acodec libmp3lame -y amix-no-volume.mp3

ffmpeg -vn -i test.mp3 -i test.mp3 -i test.mp3 -filter_complex "[0]adelay=0|0,volume=3[a];[1]adelay=2000|2000,volume=2[b];[2]adelay=4000|4000,volume=1[c];[a][b][c]amix=inputs=3:dropout_transition=0" -q:a 1 -acodec libmp3lame -y amix-volume.mp3

I can't really understand why amix changes the volume; anyway; I was digging around since a while for a good solution.

查看更多
登录 后发表回答