me mostre qual é o erro neste código que nao gera os videos: from moviepy.editor import…
- User
me mostre qual é o erro neste código que nao gera os videos:
from moviepy.editor import VideoFileClip from pytube import YouTube from pydub import AudioSegment import numpy as np import os import whisper import cv2 import re
Função para baixar o vídeo do YouTube com preferência por 1080p ou 720p
def download_video(url, output_path='video.mp4'): print("Baixando vídeo do YouTube...") yt = YouTube(url) stream = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first() if stream is None or ('1080p' not in stream.resolution and '720p' not in stream.resolution): raise ValueError("O vídeo não está disponível em 1080p ou 720p") stream.download(filename=output_path) print(f'Vídeo baixado para {output_path}') return yt.title
Função para extrair o áudio do vídeo
def extract_audio(video_path, audio_path='audio.wav'): print("Extraindo áudio do vídeo...") video = VideoFileClip(video_path) audio = video.audio audio.write_audiofile(audio_path) print(f'Áudio extraído para {audio_path}') audio.close() video.close()
Função para transcrever o áudio usando Whisper
def transcribe_audio_whisper(audio_path): print("Transcrevendo áudio usando Whisper...") model = whisper.load_model("base") result = model.transcribe(audio_path) print("Transcrição completa.") return result['text'], result['segments']
Função para analisar a transcrição e identificar segmentos com ideias concluídas
def analyze_transcription(segments): print("Analisando transcrição para identificar segmentos com ideias concluídas...") conversation_segments = [] current_segment = "" segment_start = 0
for i, segment in enumerate(segments): text = segment['text'] start = segment['start'] end = segment['end'] # Verificar se o texto contém uma ideia ou conclusão if has_idea(text): conversation_segments.append({ 'text': text, 'start': start, 'end': end }) print(f"Ideia identificada: {text}") print(f"Identificados {len(conversation_segments)} segmentos com ideias concluídas.") return conversation_segmentsFunção para verificar se o texto contém uma ideia ou conclusão
def has_idea(text): # Regex para identificar frases que podem conter uma ideia ou conclusão idea_patterns = [ r".\b(\w+\s+){2,}\w+\b[.?!]?$", # Frase com pelo menos três palavras e pontuação no final r".\bporque\b.+" # Frase com a palavra "porque" ]
for pattern in idea_patterns: if re.match(pattern, text, re.IGNORECASE): return True return FalseFunção para redimensionar e ajustar o vídeo para a resolução vertical 1080x1920
def resize_and_crop_vertical(clip): target_width = 1080 target_height = 1920
def process_frame(frame): frame_height, frame_width = frame.shape[:2] aspect_ratio = frame_width / frame_height target_aspect_ratio = target_width / target_height if aspect_ratio > target_aspect_ratio: # Se o vídeo for mais largo, redimensionar pela altura e cortar a largura new_height = target_height new_width = int(new_height * aspect_ratio) frame = cv2.resize(frame, (new_width, new_height)) x_start = (new_width - target_width) // 2 frame = frame[:, x_start:x_start + target_width] else: # Se o vídeo for mais alto, redimensionar pela largura e cortar a altura new_width = target_width new_height = int(new_width / aspect_ratio) frame = cv2.resize(frame, (new_width, new_height)) y_start = (new_height - target_height) // 2 frame = frame[y_start:y_start + target_height, :] return frame # Aplicar o redimensionamento e corte a cada frame do vídeo processed_clip = clip.fl_image(process_frame) return processed_clip - Assistant