我正试图将wav文件列表合并到一个音频文件中。到目前为止,这就是我所拥有的。
我不知道如何把这些物体加起来,因为它们都是一个物体。

import glob, os
from pydub import AudioSegment

wavfiles = []
for file in glob.glob('*.WAV'):
    wavfiles.append(file)

outfile = "sounds.wav"

pydubobjects = []

for file in wavfiles:
    pydubobjects.append(AudioSegment.from_wav(file))


combined_sounds = sum(pydubobjects) #this is what doesn't work of course

# it should be like so
# combined_sounds = sound1 + sound2 + sound 3
# with each soundX being a pydub object

combined_sounds.export(outfile, format='wav')

最佳答案

sum函数因其starting value defaults to 0而失败,您不能添加AudioSegment和整数。
您只需要添加一个起始值,如下所示:

combined_sounds = sum(pydubobjects, AudioSegment.empty())

此外,如果只想合并文件(不需要中间的文件名列表或AudioSegment对象),实际上不需要单独的循环:
import glob
from pydub import AudioSegment

combined_sound = AudioSegment.empty()
for filename in glob.glob('*.wav'):
    combined_sound += AudioSegment.from_wav(filename)

outfile = "sounds.wav"
combined_sound.export(outfile, format='wav')

10-08 16:32