Altera background de branco para preto, altera cor da letra para branco, cria um auto-resize para formatar os textos com quebras de linhas

This commit is contained in:
Leonardo Mortari
2025-08-04 09:03:34 -03:00
parent 1e15544687
commit 9c626a1e4a

View File

@@ -7,6 +7,31 @@ from moviepy import TextClip
font = "./Montserrat.ttf" 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): def cut_video_new_clip(input_path: str, start: float, end: float, output_path: str):
video_codec = "libx264" 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: with VideoFileClip(input_path) as clip:
dur = clip.duration 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) video_resized = clip.resized(width=final_width)
y = top_h + (middle_h - video_resized.h) // 2 y = top_h + (middle_h - video_resized.h) // 2
video_resized = video_resized.with_position((0, y)) video_resized = video_resized.with_position((0, y))
video_codec = "libx264" 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( txt_top = TextClip(
text=top_text, text=wrapped_top_text,
font_size=70, font_size=70,
color="black", color="white",
font=font, font=font,
method="label", method="label",
size=(final_width, top_h) size=(final_width, top_h)
).with_duration(dur).with_position((0, 0)) ).with_duration(dur).with_position((0, 0))
txt_bot = TextClip( txt_bot = TextClip(
text=bottom_text, text=wrapped_bottom_text,
font_size=70, font_size=70,
color="black", color="white",
font=font, font=font,
method="label", method="label",
size=(final_width, bottom_h), size=(final_width, bottom_h),