From a9ab813589e1af601979efbffd8d71a36d97eeef Mon Sep 17 00:00:00 2001 From: Paillat Date: Fri, 3 May 2024 09:18:05 +0200 Subject: [PATCH] :sparkles: feat(VideoBackgroundEngine): Loop video if too short --- src/engines/BackgroundEngine/VideoBackgroundEngine.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/engines/BackgroundEngine/VideoBackgroundEngine.py b/src/engines/BackgroundEngine/VideoBackgroundEngine.py index bf07d60..a2e6f9c 100644 --- a/src/engines/BackgroundEngine/VideoBackgroundEngine.py +++ b/src/engines/BackgroundEngine/VideoBackgroundEngine.py @@ -42,11 +42,10 @@ class VideoBackgroundEngine(BaseBackgroundEngine): def get_background(self): background = mp.VideoFileClip(f"{self.background_video.path}", audio=False) + if background.duration < self.ctx.duration: + num = int(self.ctx.duration / background.duration) + 1 + background = mp.concatenate_videoclips([background for _ in range(num)]) background_max_start = background.duration - self.ctx.duration - if background_max_start < 0: - raise ValueError( - "The background video is shorter than the video to be generated." - ) start = random.uniform(0, background_max_start) clip = background.with_subclip(start, start + self.ctx.duration) self.ctx.credits += f"\n{self.background_video.data['credits']}" @@ -58,6 +57,7 @@ class VideoBackgroundEngine(BaseBackgroundEngine): else: clip = clip.with_effects([vfx.Resize(width=self.ctx.width)]) clip = clip.with_position(("center", "center")) + clip: mp.VideoClip = clip.without_audio() return clip @classmethod