python 使用 wave 库分割音频 split audio

创建日期: 2024-03-03 09:32 | 作者: 风波 | 浏览次数: 17 | 分类: Python

来源: - https://blog.csdn.net/qq_40728667/article/details/133897029 - 音频分割 - https://stackoverflow.com/questions/50517253/joining-wav-files-without-writing-on-disk-in-python - 读写内存中的数据

1. 音频分割

import wave
import numpy as np
from scipy import signal

wf = wave.open("audio.wav", "rb")
nchannels = wf.getnchannels()
sampwidth = wf.getsampwidth()
framerate = wf.getframerate()
nframes = wf.getnframes()

duration = nframes / framerate
print("音频文件时长:%.2fs" % duration)

# 设置分割的长度为2s
length = 2 * framerate
start = 0

for i in range(5):
    # 截取片段
    wf.setpos(start)
    data = wf.readframes(length)

    # 保存为新文件
    new_wf = wave.open("segment_%d.wav" % i, "wb")
    new_wf.setnchannels(nchannels)
    new_wf.setsampwidth(sampwidth)
    new_wf.setframerate(framerate)
    new_wf.writeframes(data)
    new_wf.close()

    # 更新起始位置
    start += length

2. 读写内存数据

audio = [binary_wav1, binary_wav2,..., binary_wavN] # a list of .wav binary files coming from a socket
audio = [io.BytesIO(x) for x in audio]

# Join wav files

params_set = False
temp_file = io.BytesIO()
with wave.open(temp_file, 'wb') as temp_input:
    for audio_file in audio:
        with wave.open(audio_file, 'rb') as w:
            if not params_set:
                temp_input.setparams(w.getparams())
                params_set = True
            temp_input.writeframes(w.readframes(w.getnframes()))

#move the cursor back to the beginning of the "file"
temp_file.seek(0)
# Do speech recognition
binary_audio = temp_file.read()
ASR(binary_audio)
17 浏览
13 爬虫
0 评论