Skip to content

src/adjust_audio_volume.py

Adjust the volume of audio files in a directory to a target dBFS.

Source code in src/adjust_audio_volume.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def main():
    """
    Adjust the volume of audio files in a directory to a target dBFS.
    """
    src_dir = "output"
    dest_dir = "output_adjusted"
    target_dbfs = -14

    for filename in os.listdir(src_dir):
        if filename.endswith(".mp3"):
            src_path = os.path.join(src_dir, filename)
            dest_path = os.path.join(dest_dir, filename)

            audio = AudioSegment.from_mp3(src_path)
            current_dbfs = audio.dBFS
            adjusted_audio = audio.apply_gain(target_dbfs - current_dbfs)
            adjusted_audio.export(dest_path, format="mp3")

            src_id3 = ID3(src_path)
            dest_id3 = ID3(dest_path)
            dest_id3.update(src_id3)
            dest_id3.save()