PENSE PASSO A PASSO NISSO ATÉ ENCONTRAR A SOLUÇÃO PRECISA: Esse é o arquivo legenda.srt:…
- User
PENSE PASSO A PASSO NISSO ATÉ ENCONTRAR A SOLUÇÃO PRECISA:
Esse é o arquivo legenda.srt:
[00:17:772] Eu [00:18:672] sei [00:19:972] que [00:20:432] tenho [00:21:732] um [00:22:032] tempo [00:23:032] para [00:23:532] sofrer, [00:25:432] mas [00:25:932] sei [00:27:332] também [00:29:032] que [00:29:632] eu [00:30:032] vou [00:30:832] vencer. [00:33:032] Jesus [00:34:932] assim [00:36:732] falou. [00:40:232] Eu [00:40:532] venciPreciso que modifique o código por completo para que o vermelho passe de forma precisa com base nos valores
00:17:772até o próximo valor00:18:672e assim por diante.[MANDE O CÓDIGO COMPLETO E JÁ CORRIGIDO]
SCRIPT:
import re from datetime import datetime, timedelta from PIL import Image, ImageDraw, ImageFont, ImageColor import subprocess import os import multiprocessing import math import numpy as np font_size = 35 image_size = (1280, 720) font_color = (255, 0, 0) # Vermelho highlight_color = (255, 255, 255) # Branco upper_case = True fps = 20 word_spacing = 22 def parse_time(time_str): match = re.match(r'(\d+):(\d+):(\d+)', time_str) if match: minutes, seconds, milliseconds = map(int, match.groups()) time = timedelta(minutes=minutes, seconds=seconds, milliseconds=milliseconds) return time return timedelta(0) def read_subtitles(filename): subtitles = [] with open(filename, 'r', encoding='utf-8') as file: for line in file: match = re.match(r'\[(\d+:\d+:\d+)\] (.+)', line.strip()) if match: time_str, word = match.groups() subtitles.append((parse_time(time_str), word)) return subtitles def generate_frames_chunk(args): start_frame, end_frame, subtitles, output_dir, font, image_size, highlight_color, font_color, fps, upper_case = args font_path = os.path.join(os.path.dirname(__file__), "impact.ttf") font = ImageFont.truetype(font_path, font_size) background_image = Image.open(r"C:\Users\lucas\OneDrive\Documentos\capa.jpg").convert("RGB") background_image = background_image.resize(image_size) for frame_index in range(start_frame, end_frame): current_time = timedelta(seconds=frame_index / fps) image = background_image.copy() draw = ImageDraw.Draw(image) x = (image_size[0] - font.getlength(' '.join(word for _, word in subtitles))) // 2 y = image_size[1] // 2 - font_size // 2 for i, (word_time, word) in enumerate(subtitles): if upper_case: word = word.upper() if current_time < word_time: color = highlight_color elif i + 1 < len(subtitles) and current_time < subtitles[i + 1][0]: word_duration = (subtitles[i + 1][0] - word_time).total_seconds() word_progress = min(1, max(0, (current_time - word_time).total_seconds() / word_duration)) gradient_image = create_gradient_word(word, font, font.getlength(word), font_size, font_color, highlight_color, word_progress) image.paste(gradient_image, (int(x), int(y)), gradient_image) else: color = font_color draw.text((x, y), word, font=font, fill=color) x += font.getlength(word + ' ') frame_filename = os.path.join(output_dir, f"frame_{frame_index:04d}.jpg") image.save(frame_filename) def create_gradient_word(word, font, width, height, start_color, end_color, progress): word_image = Image.new('RGBA', (int(width), height), (0, 0, 0, 0)) word_draw = ImageDraw.Draw(word_image) for i in range(int(width)): char_progress = i / width if char_progress < progress: color = start_color else: color = end_color word_draw.line([(i, 0), (i, height)], fill=color) # Aplicar máscara de texto mask = Image.new('L', (int(width), height), 0) mask_draw = ImageDraw.Draw(mask) mask_draw.text((0, 0), word, font=font, fill=255) word_image.putalpha(mask) return word_image def generate_frames_parallel(subtitles, output_dir): font_path = os.path.join(os.path.dirname(__file__), "impact.ttf") font = ImageFont.truetype(font_path, font_size) total_duration = subtitles[-1][-1][0].total_seconds() + 0.5 # Tempo extra adicionado para a última palavra num_frames = int(total_duration * fps) + 1 num_processes = multiprocessing.cpu_count() chunk_size = num_frames // num_processes + 1 with multiprocessing.Pool() as pool: args = [ (i * chunk_size, min((i + 1) * chunk_size, num_frames), subtitles, output_dir, font, image_size, highlight_color, font_color, fps, upper_case) for i in range(num_processes) ] pool.map(generate_frames_chunk, args) def create_video(frame_folder, output_video, framerate, audio_file): frame_pattern = os.path.join(frame_folder, 'frame_%04d.jpg') command = [ 'ffmpeg', '-y', '-framerate', str(framerate), '-i', frame_pattern, '-i', audio_file, '-c:v', 'h264_nvenc', '-preset', 'slow', '-crf', '23', '-c:a', 'aac', '-strict', 'experimental', '-shortest', output_video ] subprocess.run(command, check=True) if __name__ == "__main__": subtitles = read_subtitles('legenda.srt') output_dir = 'frames' os.makedirs(output_dir, exist_ok=True) generate_frames_parallel(subtitles, output_dir) create_video(output_dir, 'output_video.mp4', fps, 'audio.wav') - Assistant