Reputation: 112
First of all, this is a question asked more out of curiosity than desperate need of fixing. I seem to have "fixed" it by upgrading from FFmpeg 3.4.6 to FFmpeg 4.3.1. Or so I think, at least the result is much better.
I'm trying to mix 2 audio files (of the same length, so no scale variation here) using FFmpeg amix
filter:
Now, I learnt that the filter will divide each input volume by 1/nb_active_inputs
(by 2 in my case then), so I added a volume filter on the output to multiply its volume by 2. My command basically looks like this:
ffmpeg -i music.mp3 -i voice.mp3 -filter_complex amix=inputs=2:duration=first[mix];[mix]volume=2[out] -map [out] output.mp3
After that complex filter, my final output is completely distorted. The voice sounds overall very loud and saturated.
I'm not an expert with audio nor with FFmpeg so I assume I'm missing something. I'm trying to understand what is going on and why I am having this result since I use the same inputs, filters and filter options in both cases. Only the FFmpeg version differs.
I've tried to read the code of af_amix.c
for both versions but can't seem to find any explanation for that difference. I've seen that the 4.3.1 has a weights
option for inputs but from what I understood, they default to 1.0
if not specified. Any clue would be much appreciated.
Upvotes: 3
Views: 904
Reputation: 1809
You can set the volume of both mp3 files accordingly and generate new mp3 file(output.mp3) like this:
<?php
$path = dirname(__FILE__); // Absolute directory path
$music = $path.'/background.mp3'; // Absolute mp3 input file path
$voice = $path.'/voice.mp3'; // Absolute mp3 input file path
$output = $path.'/output.mp3'; // Absolute mp3 output file path
$voice_volume = '1bB';
$bg_music_volume = '-10bB'; // -(minus) denotes decrease volume.
echo shell_exec('ffmpeg -i '.$music.' -i '.$voice.' -filter_complex "[0:0][1:0]concat=n=2:v=0:a=1,volume='.$bg_music_volume.',aformat=fltp,pan=stereo|c0=c0|c1=c1[a0];[1]volume='.$voice_volume.',aformat=fltp,pan=stereo|c0=c0|c1=c1[a1];[a0][a1]amerge,aformat=fltp[a]" -map "[a]" -strict -2 -y '.$output.'');
?>
Default Volume:
You can adjust the volume accordingly
Upvotes: 1
Reputation: 39
You have to set number of input and output method of sound reproduction(stereo,mono) and set volume one by one like this:
ffmpeg -i $firstAudio -i $secondAudio -filter_complex [0:a]volume=$firstAudioVolume[A];[1:a]volume=$secondAudioVolume[B];[A][B]amerge=inputs=2,pan=stereo|c0<c0+c2|c1<c1+c3 $outputAudio
and this sample for using:
ffmpeg -i music.mp3 -i voice.mp3 -filter_complex '[0:a]volume=3.0[A];[1:a]volume=1.0[B];[A][B]amerge=inputs=2,pan=stereo|c0<c0+c2|c1<c1+c3' output.mp3
Upvotes: 1