I am new to the audio processing scene. I have a set of timestamps generated by a speech parsing program. What I want to do now is to break up the full wav file into segments specified by the list of timestamps. Can someone recommend a python library I can use for this job?
One (of the many) solutions would be to use SciPy:
from scipy.io import wavfile
# the timestamp to split at (in seconds)
split_at_timestamp = 42
# read the file and get the sample rate and data
rate, data = wavfile.read('foo.wav')
# get the frame to split at
split_at_frame = rate * split_at_timestamp
# split
left_data, right_data = data[:split_at_frame-1], data[split_at_frame:] # split
# save the result
wavfile.write('foo_left.wav', rate, left_data)
wavfile.write('foo_right.wav', rate, right_data)
pydub has easier methods to split audio files of different formats (wav, mp3 etc) between two intervals.
Here's sample code
from pydub import AudioSegment
audio_file= "your_wav_file.wav"
audio = AudioSegment.from_wav(audio_file)
list_of_timestamps = [ 10, 20, 30, 40, 50 ,60, 70, 80, 90 ] #and so on in *seconds*
start = ""
for idx,t in enumerate(list_of_timestamps):
#break loop if at last element of list
if idx == len(list_of_timestamps):
break
end = t * 1000 #pydub works in millisec
print "split at [ {}:{}] ms".format(start, end)
audio_chunk=audio[start:end]
audio_chunk.export( "audio_chunk_{}.wav".format(end), format="wav")
start = end * 1000 #pydub works in millisec
Result:
split at [ :10000] ms
split at [ 10000000:20000] ms
split at [ 20000000:30000] ms
split at [ 30000000:40000] ms
split at [ 40000000:50000] ms
split at [ 50000000:60000] ms
split at [ 60000000:70000] ms
split at [ 70000000:80000] ms
split at [ 80000000:90000] ms
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With