Skip to content Skip to sidebar Skip to footer

Python Silence Specific Part Of Audio File

I have an audio file audio.wav and I have an array of time frames looking like this one: X = [(12.31, 14.), (15.4, 18.9), ...] These are the time frames that I would like to be com

Solution 1:

Based on your link I see it as

from pydub import AudioSegment

a = AudioSegment.from_wav("audio.wav")

# X = [(12.31, 14.), (15.4, 18.9), ...]

duration = (14.0 - 12.31) * 1000
s1 = AudioSegment.silent(duration)

duration = (18.9 - 15.4) * 1000
s2 = AudioSegment.silent(duration)

b = a[:12310] + s1 + a[14000:15400] + s2 + a[18900:]

b.export('new_audio.wav', format='wav')

now problem is to use for-loop to automate it

I can't test it but I see it as

from pydub import AudioSegment

a = AudioSegment.from_wav("audio.wav")

X = [(12.31, 14.), (15.4, 18.9)]

parts = []

# start for audiobegin = 0for start, endinX:# keep sound before silence
    s = a[begin*1000:start*1000]
    parts.append(s)  
    
    # create silence
    duration = (end - start) * 1000
    s = AudioSegment.silent(duration)
    parts.append(s)

    # value for next loopbegin = end# keep part after last silence
parts.append(a[begin*1000:])

# join all parts using standard `sum()` but it need `parts[0]` as start value   
b = sum(parts[1:], parts[0])

# save it
b.export('new_audio.wav', format='wav')

Post a Comment for "Python Silence Specific Part Of Audio File"