How to normalize the volume of an audio file in python: any packages currently available?
You can use pydub module to achieve normalization of peak volume with least amount of code.
Install pydub
using
pip install pydub
Inspiration from here
You can measure rms in pydub
which is a measure of average amplitude, which pydub provides as audio_segment.rms. It also provides a convenient method to convert values to dBFS (audio_segment.dBFS)
If you want an audio file to be the same average amplitude, basically you choose an average amplitude (in dBFS, -20 in the example below), and adjust as needed:
from pydub import AudioSegment
def match_target_amplitude(sound, target_dBFS):
change_in_dBFS = target_dBFS - sound.dBFS
return sound.apply_gain(change_in_dBFS)
sound = AudioSegment.from_file("yourAudio.m4a", "m4a")
normalized_sound = match_target_amplitude(sound, -20.0)
normalized_sound.export("nomrmalizedAudio.m4a", format="mp4")
from pydub import AudioSegment, effects
rawsound = AudioSegment.from_file("./input.m4a", "m4a")
normalizedsound = effects.normalize(rawsound)
normalizedsound.export("./output.wav", format="wav")
Before:
After: