无法使用 moviepy 合并多个音频文件
Not able to merge several audio files using 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'
我哪里错了?
正如 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')
我的目录中有大约 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'
我哪里错了?
正如 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')