我正在使用此代码在Python中将音频文件添加到视频文件中,但是,当视频结束时,音频会一直播放到最后。我想在视频结束时结束。
有什么建议吗?
from moviepy.editor import *
videoclip = VideoFileClip(filename)
audioclip = AudioFileClip("audio.mp3")
new_audioclip = CompositeAudioClip([audioclip])
videoclip.audio = new_audioclip
videoclip.write_videofile("new_filename.mp4")
答案 0 :(得分:3)
afx代替CompositeAudioClip,对我有效:
from moviepy.editor import *
videoclip = VideoFileClip(filename)
audioclip = AudioFileClip("audio.mp3")
new_audio = afx.audio_loop(audioclip, duration=videoclip.duration)
videoclip.audio = new_audio
videoclip.write_videofile("new_filename.mp4")
答案 1 :(得分:2)
您应该获取视频时长(长度)并将其用于裁剪音频。
而且您不需要CompositeAudioClip
。
from moviepy.editor import *
videoclip = VideoFileClip(filename)
audioclip = AudioFileClip("audio.mp3")
videoclip.audio = audioclip.set_duration(videoclip.duration)
videoclip.write_videofile("new_filename.mp4")