1

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')
funie200
  • 3,688
  • 5
  • 21
  • 34

1 Answers1

1

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')
funie200
  • 3,688
  • 5
  • 21
  • 34
  • This works going through the audio files. though it will give an error when saving the padded files – Mugagga mycle Nov 02 '20 at 12:37
  • TypeError: can only concatenate list (not "str") to list – Mugagga mycle Nov 02 '20 at 12:57
  • also where is `full_path` defined ? – Scott Stensland Nov 02 '20 at 13:02
  • @Mugaggamycle Oops, that was in the `split` function, should be fixed now. However, you maybe want to change that to something else if you like, I just looked at the filename you had in your example – funie200 Nov 02 '20 at 13:27
  • @ScottStensland No idea, it wasn't defined in the code in the question either – funie200 Nov 02 '20 at 13:27
  • 2
    @ScottStensland, i havent had any red flags when running the code without defining full_path, seems like it couldbe defined in the pydub lib. this is where i got the code from https://stackoverflow.com/questions/52841335/how-can-i-pad-wav-file-to-specific-length – Mugagga mycle Nov 02 '20 at 13:45