How to normalize the volume of an audio file in python?

Luke Davis picture Luke Davis · Feb 27, 2017 · Viewed 16.6k times · Source

I have a bunch of AAC (.m4a) audio files that need to be normalized, and was hoping to find a way to do it with a simple python script using some package.

I found this thread on superuser where someone has written an ffmpeg command-line utility in python, and it works well, but was wondering if there is some currently available python package with a pip install that would be up to the task.

Answer

Anil_M picture Anil_M · Feb 27, 2017

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")