background = Image.open("background.jpg")
new_background = background.resize((screen_width, screen_height))
new_background.save("temp/resized_background.png")
# Maybe I could speed this up by only resizing the background once, but that is irrelevant to this issue
image_clip = ImageClip(f"temp/resized_background.png")
audio_clip = AudioFileClip(f'temp/{random_file_name}.mp3')
video_clip = image_clip.set_audio(audio_clip)
video_clip.duration = audio_clip.duration
video_clip.fps = fps # fps=12
# Memory cleanup
del background
del new_background
del image_clip
del audio_clip
return video_clip
第三个剪辑由以下代码创建:
clips = []
for pic, audioFile in zip(self.images, self.audioFiles):
audio_clip = AudioFileClip(f"temp/{audioFile}.mp3")
image_clip = ImageClip(f"temp/{pic}.png").resize((screen_height, screen_width))
# The images should already be this size, but also irrelevant here
except:
continue
video_clip = image_clip.set_audio(audio_clip)
video_clip.duration = audio_clip.duration
video_clip.fps = self.fps
clips.append(video_clip)
# Memory management
del image_clip
del audio_clip
del video_clip
unsilenced_video = concatenate_videoclips(clips, method="compose")
del clips
self.duration = unsilenced_video.duration
self.fullClip = unsilenced_video
del unsilenced_video
return self
然后,我确保所有这些都按照正确的顺序排列在一个列表中。然后我把它们连在一起
concat_clip = concatenate_videoclips(allClipsInOrder, method="compose")
# Put soft background music over the whole video
background = AudioFileClip("background_music.mp3").volumex(0.1)
full_background = afx.audio_loop(background, duration=concat_clip.duration)
final_audio = CompositeAudioClip([concat_clip.audio, full_background])
self.full_video = concat_clip.set_audio(final_audio)
allClipsInOrder = [VideoFileClip(vidPath) for vidPath in self.clipFilePaths]
concat_clip = concatenate_videoclips(allClipsInOrder, method="compose")
# Put soft background music over the whole video
background = AudioFileClip("background_music.mp3").volumex(0.1)
full_background = afx.audio_loop(background, duration=concat_clip.duration)
final_audio = CompositeAudioClip([concat_clip.audio, full_background])