pydub Overlay Delay

1.4k Views Asked by At

I'm trying to create a mix out of two audio tracks (vocal and instrumental) that are of the same duration (3.:30). However, when I try to use the overlay function, my vocal starts too soon.

from pydub import AudioSegment

sound1 = AudioSegment.from_file("vocals.mp3")
sound2 = AudioSegment.from_file("instrumental.mp3")
combined = sound1.overlay(sound2)
combined.export("mix.mp3", format='mp3')
1

There are 1 best solutions below

0
On

I believe you can play with the position of overlay where def overlay(self, seg, position=0, loop=False, times=None, gain_during_overlay=None) the position parameter means where on the original sound do you wish to start.

# This takes the duration of sound2 and overlays
# sound1 after 10% of the song2's duration.
combined = sound2.overlay(sound1, position=0.1*len(sound2))

Note that this will make your total song be as long as sound1, to fix that you can add the rest of the song:

if len(sound2) < len(sound1):
   # Play the last 10% of the song1
   combined += sound2[-1.1*len(sound1)+len(sound2):]