我正在编写一个程序,它从一个文件夹中获取一堆视频,编译它们,然后从一个文件夹中获取另一组音频文件,并将它们连接起来,然后将合并的音频覆盖在最终视频的顶部(希望这是有意义的)。我能够将视频处理成一个最终的输出视频,但是我在合并音频时经常遇到麻烦。下面我提供了两小部分关于音频的代码,我正在使用moviepy来处理视频,并尝试将其用于音频。
songDirectory = './songs/'
songList = []
songs = []
audioclip=''
def makeCompilation():
for filename in os.listdir(songDirectory):
f = os.path.join(songDirectory, filename)
if os.path.isfile(f) and filename.endswith(".mp3"):
audioclip = moviepy.editor.AudioFileClip(f)
songList.append(filename)
for song in os.listdir(songDirectory):
c = os.path.join(songDirectory, song)
audio_clips = moviepy.editor.AudioFileClip(c)
audio_output = moviepy.editor.concatenate_audioclips(audio_clips)
finalClip = concatenate_videoclips(videos, method="compose")
final_duration = finalClip.duration
final_audio_output = audio_output.set_duration(final_duration)
final_output= finalClip.set_audio(final_audio_output).fx(afx.audio_fadein, 3.0)
audio_path = "./songs/tempaudiofile.m4a"
#print(description)
# Create compilation
final_output.write_videofile(outputFile, threads=8, temp_audiofile=audio_path, remove_temp=True, codec="libx264", audio_codec="aac")
return description该程序似乎能够找到音频目录,但我需要能够使用os.path.join(songDirectory,文件名)直接指向每个mp3文件,所以在得到上面的代码之前我一直在处理它。当我试图在songList中迭代时,正如我所料,我收到了一个错误,它说'str‘是不可迭代的,而其他错误则说'str’没有属性‘持续性’。本质上,我所需要做的就是通过输入文件夹进行迭代,并以任何方式组合文件。代码当前返回输出:
Free Fredobagz x Aflacko x Flint type beat - 'Default_Dance' [prod. kah]-jro0v6ogZ0Y.mp4
225.05
Total Length: 225.05
225.05
Traceback (most recent call last):
File "/Users/daddyK/Desktop/comp_ bot/make_compilation3.py", line 127, in <module>
makeCompilation(path = "./videos/",
File "/Users/daddyK/Desktop/comp_ bot/make_compilation3.py", line 110, in makeCompilation
audio_output = moviepy.editor.concatenate_audioclips(audio_clips)
File "/Users/daddyK/Library/Python/3.10/lib/python/site-packages/moviepy/audio/AudioClip.py", line 315, in concatenate_audioclips
durations = [c.duration for c in clips]
TypeError: 'AudioFileClip' object is not iterable在这一点上,我有点困惑,所以如果有人可以提供一些洞察力,我应该做什么来解决错误和/或如果我是朝着正确的方向代码-我会非常感激!对不起,如果代码没有任何意义,如果需要的话,我会发布整个.py文件
发布于 2022-04-25 11:57:15
根据@kesh的评论,您需要替换
songList.append(filename)使用
songList.append(audioclip)https://stackoverflow.com/questions/71983450
复制相似问题