python 拼接 wav 音频文件

创建日期: 2024-02-23 15:37 | 作者: 风波 | 浏览次数: 14 | 分类: Python

来源:https://stackoverflow.com/questions/2890703/how-to-join-two-wav-files-using-python

方法一

import wave

infiles = ["sound_1.wav", "sound_2.wav"]
outfile = "sounds.wav"

data= []
for infile in infiles:
    w = wave.open(infile, 'rb')
    data.append( [w.getparams(), w.readframes(w.getnframes())] )
    w.close()

output = wave.open(outfile, 'wb')
output.setparams(data[0][0])
for i in range(len(data)):
    output.writeframes(data[i][1])
output.close()

方法二

from pydub import AudioSegment

sound1 = AudioSegment.from_wav("/path/to/file1.wav")
sound2 = AudioSegment.from_wav("/path/to/file2.wav")

combined_sounds = sound1 + sound2
combined_sounds.export("/output/path.wav", format="wav")
14 浏览
13 爬虫
0 评论