diff --git a/components/video.py b/components/video.py index d689236..7a0658d 100644 --- a/components/video.py +++ b/components/video.py @@ -7,6 +7,31 @@ from moviepy import TextClip font = "./Montserrat.ttf" + +def auto_wrap_text(text, font, font_size, max_width): + if not text: + return "" + + words = text.split() + lines = [] + + line = "" + for word in words: + test_line = f"{line} {word}".strip() + test_clip = TextClip(test_line, font=font, font_size=font_size, color='white', method='label') + + if test_clip.w > max_width and line != "": + lines.append(line) + line = word + else: + line = test_line + + test_clip.close() + + lines.append(line) + + return "\n".join(lines) + def cut_video_new_clip(input_path: str, start: float, end: float, output_path: str): video_codec = "libx264" @@ -38,25 +63,27 @@ def process_segment(input_path: str, top_text: str = "", bottom_text: str = "", with VideoFileClip(input_path) as clip: dur = clip.duration - bg = ColorClip(size=(final_width, final_height), color=(255, 255, 255), duration=dur) + bg = ColorClip(size=(final_width, final_height), color=(0, 0, 0), duration=dur) video_resized = clip.resized(width=final_width) y = top_h + (middle_h - video_resized.h) // 2 video_resized = video_resized.with_position((0, y)) video_codec = "libx264" + wrapped_top_text = auto_wrap_text(top_text, font, font_size, final_width - 40) + wrapped_bottom_text = auto_wrap_text(bottom_text, font, font_size, final_width - 40) txt_top = TextClip( - text=top_text, + text=wrapped_top_text, font_size=70, - color="black", + color="white", font=font, method="label", size=(final_width, top_h) ).with_duration(dur).with_position((0, 0)) txt_bot = TextClip( - text=bottom_text, + text=wrapped_bottom_text, font_size=70, - color="black", + color="white", font=font, method="label", size=(final_width, bottom_h),