无法使用moviepy合并多个音频文件

时间:2020-08-24 15:02:53

标签: python audio merge webm moviepy

我的目录中大约有245个.webm文件。我想将所有245个文件合并到一个名为output.webm的文件中。我正在使用python的“ moviepy”模块来实现这一目标。

这是我到目前为止所做的:

import os
from moviepy.editor import *

audio_folder='path/to/directory'

audio_files = [audio_folder+'/'+img for img in os.listdir(audio_folder) if img.endswith(".webm")]

print(audio_files)
audio = concatenate_audioclips(audio_files)
audio.write_audiofile('output.webm')

执行此操作时出现以下错误:

Traceback (most recent call last):
File "demo.py", line 17, in <module>
 audio = concatenate_audioclips(audio_files)
File "C:\Users\abc\AppData\Local\Programs\Python\Python38-32\lib\site- 
 packages\moviepy\audio\AudioClip.py", line 315, in concatenate_audioclips
durations = [c.duration for c in clips]
File "C:\Users\abc\AppData\Local\Programs\Python\Python38-32\lib\site- 
 packages\moviepy\audio\AudioClip.py", line 315, in <listcomp>
 durations = [c.duration for c in clips]
AttributeError: 'str' object has no attribute 'duration'

我要去哪里错了?

1 个答案:

答案 0 :(得分:0)

如Paul M上面所建议,我正在将字符串列表传递给concatenate_audioclips 函数而不是list of AudioClip Objects。这就是我解决问题的方法。

audio_folder='path/to/directory'

audio_files = [audio_folder+'/'+img for img in os.listdir(audio_folder) if img.endswith(".webm")]

print(audio_files)

audios = []
for audio in audio_files :
    audios.append(AudioFileClip(audio))

audioClips = concatenate_audioclips([audio for audio in audios])
audioClips.write_audiofile('output.webm')