WAV音频文件按秒切片段

wav音频文件按秒切片段

 

import wave

def split_wav_by_seconds(input_file, output_file, start_second, end_second):
    with wave.open(input_file, 'rb') as wf:
        framerate = wf.getframerate()
        num_frames = wf.getnframes()
        num_channels = wf.getnchannels()
        sample_width = wf.getsampwidth()

        start_frame = int(start_second * framerate)
        end_frame = int(end_second * framerate)

        with wave.open(output_file, 'wb') as output_wf:
            output_wf.setnchannels(num_channels)
            output_wf.setsampwidth(sample_width)
            output_wf.setframerate(framerate)

            wf.setpos(start_frame)
            output_wf.writeframes(wf.readframes(end_frame - start_frame))

        print(f"Saved {output_file}")

# 示例用法
input_file = "./vocals.wav"    # 输入音频文件名
output_file = "./output.wav"  # 输出文件名
start_second =  38           # 截取的起始秒数
end_second = 51              # 截取的结束秒数

split_wav_by_seconds(input_file, output_file, start_second, end_second)

 

posted @ 2024-05-01 14:46  酱_油  阅读(21)  评论(0编辑  收藏  举报