How to merge/layer two audio files using Python AudioSegment without it breaking the wav header

916 Views Asked by At

I am more or less following the code below to merge two audio files. It mostly works, where audio segment can export both the original files and the combined file to a folder. These play fine in finder (Mac). However, when brought into a music app like Ableton, the waveform is distorted and sounds like digital garbage. I have a feeling this is because this code is messing with the wav header.

I have also noted the combined sound is showing a bitrate of 32 in the finder file info, whereas I am specifically outputting it as bitrate='24'

Any theories?

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

# Overlay sound2 over sound1 at position 0
overlay = sound1.overlay(sound2, position=0)


# simple export
file_handle = overlay.export("output.wav", format="wav", bitrate='24')

enter image description here

1

There are 1 best solutions below

1
On

Note to others, I solved this by moving to using Sox (or PySox) instead of AudioSegment, which seams to work much more reliably with all the features I was looking for.