Skip to content Skip to sidebar Skip to footer

Combine Audio Files In Python

How can I combined multiple audio files (wav) to one file in Python? I found this: import wave infiles = ['sound_1.wav', 'sound_2.wav'] outfile = 'sounds.wav' data= [] for infil

Solution 1:

You can use the pydub module. It's one of the easiest ways to cut, edit, merge audio files using Python.

Here's an example of how to use it to combine audio files with volume control:

from pydub import AudioSegment
sound1 = AudioSegment.from_file("/path/to/sound.wav", format="wav")
sound2 = AudioSegment.from_file("/path/to/another_sound.wav", format="wav")

# sound1 6 dB louder
louder = sound1 + 6


# sound1, with sound2 appended (use louder instead of sound1 to append the louder version)
combined = sound1 + sound2

# simple export
file_handle = combined.export("/path/to/output.mp3", format="mp3")

To overlay sounds, try this:

from pydub import AudioSegment
sound1 = AudioSegment.from_file("1.wav", format="wav")
sound2 = AudioSegment.from_file("2.wav", format="wav")

# sound1 6 dB louder
louder = sound1 + 6

# Overlay sound2 over sound1 at position 0  (use louder instead of sound1 to use the louder version)
overlay = sound1.overlay(sound2, position=0)


# simple export
file_handle = overlay.export("output.mp3", format="mp3")

Full documentation here pydub API Documentation

Post a Comment for "Combine Audio Files In Python"