按时间戳分解.wav文件

问题描述 投票:1回答:2

我是音频处理领域的新手。我有一组语音解析程序生成的时间戳。我现在要做的是将完整的wav文件分解为由时间戳列表指定的段。有人可以推荐我可以用于这项工作的python库吗?

python parsing audio wav
2个回答
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)

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
© www.soinside.com 2019 - 2024. All rights reserved.