Search code examples
audioffmpegvolume

ffmpeg volumedetect returns unstable result


I am using ffmpeg to calculate the volume of my audio file, but I find that ffmpeg returns different values for different containers (like mp3 v.s. ogg)

Here is an example. I am using an ogg file from Wikimedia Commons, and converting it to mp3, then analyze the volume of both files. For ogg file, I get -3.0 dB for mean volume, but for mp3 file, I get -3.4 dB. When I use other files, I also get different max volume and histogram.

[user@localhost tmp]$ wget https://upload.wikimedia.org/wikipedia/commons/b/b9/Audio_Frequency_tone%2C_Middle_C%2C_C4%2C_261.63hz.ogg -O a.ogg 2> /dev/null
[user@localhost tmp]$ ffmpeg -i a.ogg a.mp3 2> /dev/null
[user@localhost tmp]$ ffmpeg -i a.ogg -af volumedetect tmp.ogg 2>&1 | grep volumedetect
[Parsed_volumedetect_0 @ 0x555f69f3f4a0] n_samples: 88768
[Parsed_volumedetect_0 @ 0x555f69f3f4a0] mean_volume: -3.0 dB
[Parsed_volumedetect_0 @ 0x555f69f3f4a0] max_volume: 0.0 dB
[Parsed_volumedetect_0 @ 0x555f69f3f4a0] histogram_0db: 27541
[user@localhost tmp]$ ffmpeg -i a.mp3 -af volumedetect tmp.mp3 2>&1 | grep volumedetect
[Parsed_volumedetect_0 @ 0x55fd62be4740] n_samples: 88768
[Parsed_volumedetect_0 @ 0x55fd62be4740] mean_volume: -3.4 dB
[Parsed_volumedetect_0 @ 0x55fd62be4740] max_volume: -0.0 dB
[Parsed_volumedetect_0 @ 0x55fd62be4740] histogram_0db: 21340
[user@localhost tmp]$ 

Why is this bug happening? Which one should I believe?


Solution

  • You're transcoding the audio. So, the output isn't identical to the input. You should get the same result if you transcode to a lossless format like WAV or FLAC.