在 Python 中创建一个空的 AudioSegment 文件 (pydub)

Creating an empty AudioSegment File in Python (pydub)

这里是初学者。我正在尝试遍历 mp3 文件的路径列表并合并所有 mp3:

combined_sounds = AudioSegment.from_mp3('./00.mp3')

for file in files:
  sound = AudioSegment.from_mp3(file)
  combined_sounds= combined_sounds + sound;

# ....

我需要在遍历列表之前定义 combined_sounds - 但我不想在遍历循环之前不向其中添加任何内容。现在我的小窍门是使用一个空的 mp3 文件 00.mp3,但这不是最好的解决方案。我将如何做到这一点?

我尝试了 combined_sounds = ''combined_sounds = None,但是当我进入循环并尝试将其转换为 AudioSegment 时,两者都出现错误。我可以在循环之前创建一个空的 AudioSegment 吗?

您可以使用 AudioSegment 中的 empty() 函数创建一个空白片段:

combined_sounds = AudioSegment.empty()

for file in files:
   sound = AudioSegment.from_mp3(file)
   combined_sounds += sound