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?
Asked
Active
Viewed 7,471 times
2 Answers
7
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)

Joren
- 3,068
- 25
- 44
5
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

Anil_M
- 10,893
- 6
- 47
- 74
-
4should't it be: `start = 0` and the update statment in for be: `start = end` instead of `start = end * 1000` because you are already doing `end = t * 1000` for milliseconds. From second line of output it should be like `split at [ 10000:20000] ms ; split at [ 20000:30000] ms ; so on...` and not `split at [ 10000000:20000] ms` – Swapnil B. Nov 29 '18 at 21:27
-