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

use*_*ara 5 python python-3.x pydub audiosegment

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

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

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

# ....
Run Code Online (Sandbox Code Playgroud)

我需要combined_sounds在遍历列表之前进行定义 - 但我不想在遍历循环之前将任何内容放入其中。现在我的小技巧是使用一个空的 mp3 文件00.mp3,但这不是最好的解决方案。我将如何实现这个目标?

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

小智 9

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

combined_sounds = AudioSegment.empty()

for file in files:
   sound = AudioSegment.from_mp3(file)
   combined_sounds += sound
Run Code Online (Sandbox Code Playgroud)