i'm a novice in python and i'm trying to loop in a directory with the goal of divide each wav files contained in the folder in many chunks. I've successfully looped and divided the files but when i try to export the chunks in a new folder i can't figure it out how to change the chunk names created so that all are saved successfully (without overwriting).
import os
from pydub import AudioSegment
path = ""
os.chdir(path)
audio_files = os.listdir()
for file in audio_files:
name, ext = os.path.splitext(file)
if ext == ".wav":
w = AudioSegment.from_wav(file)
chunk_length_ms = 3000
chunks = make_chunks(w, chunk_length_ms)
for i, chunk in enumerate(chunks):
chunk_name = "{0}.wav".format(name)
print ("exporting", chunk_name)
chunk.export('dir_path' + chunk_name, format="wav")
With this code only 100 chunks are saved successfully instead of 1100. the wav files have this name structure: "genre.000x.wav", so for ex. 'Blues.00001.wav' should be chunked in : 'Blues.00001a.wav','Blues.00001b.wav',ecc. How can i do this?
Here's a away of adding successive lowercase letters to each file name you export in your loop:
chunks = ['chunk1', 'chunk2', 'chunk3']
name = 'Blues.00001'
for i, chunk in enumerate(chunks):
chunk_name = "{0}{1}.wav".format(name, chr(ord('a')+i))
print ("exporting", chunk_name)
Result:
exporting Blues.00001a.wav
exporting Blues.00001b.wav
exporting Blues.00001c.wav