Mugagga mycle
Mugagga mycle

Reputation: 41

How to pad multiple audio wav files with silence

I am trying to pad audio wav files. I have found the code below. Could I get any help to apply this on multiple files with different names?

from pydub import AudioSegment

pad_ms = 1000
audio = AudioSegment.from_wav('sit.wav')
assert pad_ms > len(audio), + str(full_path)
silence = AudioSegment.silent(duration=pad_ms-len(audio)+1)

padded = audio + silence
padded.export('sit_1*.wav', format='wav')

Upvotes: 2

Views: 1255

Answers (1)

funie200
funie200

Reputation: 3908

How about looping over the files in a folder:

from pydub import AudioSegment
import os # Needed for os.listdir

pad_ms = 1000

path = "/the/path/name"

for filename in os.listdir(path): # Loop over all items in the path
    if (filename.endswith(".wav")): # Check if the file ends with .wav
        audio = AudioSegment.from_wav(filename)
        assert pad_ms > len(audio), + str(full_path)
        silence = AudioSegment.silent(duration=pad_ms-len(audio)+1)

        padded = audio + silence

        newFilename = filename.split(".")[0] + "_1.wav" # And something like this for the new name
        padded.export(newFilename, format='wav')

Upvotes: 2

Related Questions