Reputation: 115
I have many .wav
audio files which I want to split according to the segment information available for the corresponding .wav
files. Below is the sample of segment information for one audio file. The number inside the array is specified in milliseconds.
start_array = [1285, 19967, 30227, 41823]
duration_array = [14423, 7437, 10000, 6454]
I had written a code which splits audio file for every ten seconds and saves the split file with the same file name as original file with the extended number specifying the chunk number. for example, its "originalFileName_00"
for the first chunk, "originalFileName_01"
for the second chunk and so on.
Below is the code:
# read file from input dir and split them
for filename in os.listdir(inputdir):
save_file_name = filename[:-4]
myaudio = AudioSegment.from_file(inputdir+"/"+filename, "wav")
chunk_length_ms = 10000 # pydub calculates in millisec
chunks = make_chunks(myaudio, chunk_length_ms) #Make chunks of ten sec
# saving then to output dir.
for i, chunk in enumerate(chunks):
chunk_name = save_file_name+"_{0}.wav".format(i)
#print(chunk_name)
parts = chunk_name.split('_')
a = parts[-1].split('.')
b = a[0]
if(len(b) == 1):
b = "0"+b
chunk_name = parts[0]+"_"+parts[1]+"_"+b+".wav"
#print(chunk_name)
print "exporting", chunk_name
chunk.export(outputdir+"/"+chunk_name, format="wav")
Now I want to split the audio tracks according to the segment start and end (start + duration would give me the end of that segment) information available. How can I do this using the make_chunks
function? Or is there any other function I can use to do this?
Upvotes: 5
Views: 9291
Reputation: 1281
from the docs
AudioSegments are slicable using milliseconds. for example:
a = AudioSegment.from_mp3(mp3file) first_second = a[:1000] # get the first second of an mp3 slice = a[5000:10000] # get a slice from 5 to 10 seconds of an mp3
something like this should work:
for filename in os.listdir(inputdir):
save_file_name = filename[:-4]
myaudio = AudioSegment.from_file(inputdir+"/"+filename, "wav")
for i in range(len(start_array)):
chunk_data = myaudio[start_array[i]:start_array[i]+duration_array[i]]
Upvotes: 4