Using a for loop to rename processed .wav file with pydub AudioSegment function
Question:
I am trying to rename multiple files in a loop using the AudioSegment function. Below is the function for a single file (which works perfectly
from pydub import AudioSegment
audio = AudioSegment.from_wav("./OM1/BMM.wav")
new_audio = audio[1000:len(audio)-1000]
new_audio.export('newSong.wav', format="wav")
Now I want to loop through the whole folder but it seems to give me just one folder
i = 1
for wave_file in glob.glob("./OM1/*.wav"): #Folder containing subject files
sound = AudioSegment.from_wav(wave_file)
new_sound = sound[1000:len(audio)-1000]
new_sound.export('newSong.wav', format="wav")
Answers:
it looks like your variable names are mixed up. look at this:
sound = AudioSegment.from_wav(wave_file)
new_sound = sound[1000:len(audio)-1000]
new_audio.export('newSong.wav', format="wav")
it looks like your last line should be:
new_sound.export('newSong.wav', format="wav")
also, obviously all of your output files cannot be named newSong.wav
for wave_file in glob.glob("/*.wav"):
sound = AudioSegment.from_wav(wave_file)
extract = sound[10:len(audio)-10]
#extract = sound[1000:len(audio)-1000]
extract.export(wave_file+'-extract.wav', format="wav")
print(wave_file)
I am trying to rename multiple files in a loop using the AudioSegment function. Below is the function for a single file (which works perfectly
from pydub import AudioSegment
audio = AudioSegment.from_wav("./OM1/BMM.wav")
new_audio = audio[1000:len(audio)-1000]
new_audio.export('newSong.wav', format="wav")
Now I want to loop through the whole folder but it seems to give me just one folder
i = 1
for wave_file in glob.glob("./OM1/*.wav"): #Folder containing subject files
sound = AudioSegment.from_wav(wave_file)
new_sound = sound[1000:len(audio)-1000]
new_sound.export('newSong.wav', format="wav")
it looks like your variable names are mixed up. look at this:
sound = AudioSegment.from_wav(wave_file)
new_sound = sound[1000:len(audio)-1000]
new_audio.export('newSong.wav', format="wav")
it looks like your last line should be:
new_sound.export('newSong.wav', format="wav")
also, obviously all of your output files cannot be named newSong.wav
for wave_file in glob.glob("/*.wav"):
sound = AudioSegment.from_wav(wave_file)
extract = sound[10:len(audio)-10]
#extract = sound[1000:len(audio)-1000]
extract.export(wave_file+'-extract.wav', format="wav")
print(wave_file)