我是音频处理领域的新手。我有一组由语音解析程序生成的时间戳。我现在要做的是将完整的wav文件分解为时间戳列表指定的段。有人可以推荐我可以用于此工作的python库吗?
答案 0 :(得分:2)
(众多)解决方案之一是使用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)
答案 1 :(得分:0)
pydub具有更简便的方法,可以在两个间隔之间分割不同格式(wav,mp3等)的音频文件。
这里是示例代码
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
结果:
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