import tempfile import logging import os import asyncio from moviepy.editor import * import edge_tts import gradio as gr from pydub import AudioSegment # Configuración de Logs logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") # CONSTANTES DE ARCHIVOS INTRO_VIDEO = "introvideo.mp4" OUTRO_VIDEO = "outrovideo.mp4" MUSIC_BG = "musicafondo.mp3" GLITCH_SOUND = "fxsound.mp3" # Efecto de sonido para glitches EJEMPLO_VIDEO = "ejemplo.mp4" # Validar existencia de archivos for file in [INTRO_VIDEO, OUTRO_VIDEO, MUSIC_BG, GLITCH_SOUND, EJEMPLO_VIDEO]: if not os.path.exists(file): logging.error(f"Falta archivo necesario: {file}") raise FileNotFoundError(f"Falta: {file}") def eliminar_archivo_tiempo(ruta, delay=1800): def eliminar(): try: if os.path.exists(ruta): os.remove(ruta) logging.info(f"Archivo eliminado: {ruta}") except Exception as e: logging.error(f"Error al eliminar {ruta}: {e}") from threading import Timer Timer(delay, eliminar).start() async def procesar_audio(texto, voz, duracion_video, audio_original): temp_files = [] try: # Validar texto if not texto.strip(): raise ValueError("El texto para TTS no puede estar vacío.") # Dividir el texto en fragmentos si es demasiado largo def dividir_texto(texto, max_length=3000): return [texto[i:i + max_length] for i in range(0, len(texto), max_length)] fragmentos = dividir_texto(texto) audios_tts = [] for fragmento in fragmentos: # Generar TTS communicate = edge_tts.Communicate(fragmento, voz) with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_tts: try: await communicate.save(tmp_tts.name) except edge_tts.exceptions.NoAudioReceived as e: logging.error(f"Error en TTS: {str(e)}") raise ValueError("No se pudo generar el audio. Verifica tu conexión o los parámetros del TTS.") tts_audio = AudioFileClip(tmp_tts.name) temp_files.append(tmp_tts.name) audios_tts.append(tts_audio) # Combinar todos los fragmentos de TTS tts_audio_final = concatenate_audioclips(audios_tts) # Limitar TTS al video if tts_audio_final.duration > duracion_video: tts_audio_final = tts_audio_final.subclip(0, duracion_video) # Preparar música de fondo en loop bg_music = AudioSegment.from_mp3(MUSIC_BG) needed_ms = int(duracion_video * 1000) repeticiones = needed_ms // len(bg_music) + 1 bg_music = bg_music * repeticiones bg_music = bg_music[:needed_ms].fade_out(1000) with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_bg: bg_music.export(tmp_bg.name, format="mp3") bg_audio = AudioFileClip(tmp_bg.name).volumex(0.15) temp_files.append(tmp_bg.name) # Combinar audios audios = [bg_audio.set_duration(duracion_video)] if audio_original: audios.append(audio_original.volumex(0.7)) # Audio original al 70% audios.append(tts_audio_final.volumex(0.85).set_start(0)) # TTS al 85% audio_final = CompositeAudioClip(audios).set_duration(duracion_video) return audio_final except Exception as e: logging.error(f" fallo en audio: {str(e)}") raise finally: for file in temp_files: try: os.remove(file) except Exception as e: logging.warning(f"Error limpiando {file}: {e}") def aplicar_glitch(video_clip): """Aplica un efecto de glitch al video.""" def glitch_effect(frame): import numpy as np height, width, _ = frame.shape offset = np.random.randint(-10, 10) if offset > 0: offset = min(offset, height) if offset < 0: offset = max(offset, -height + 1) if offset!= 0 and height > 0: frame[offset:, :] = np.roll(frame[:-offset, :], -offset, axis=0) return frame return video_clip.fl_image(glitch_effect) async def procesar_video(video_input, texto_tts, voz_seleccionada): try: # Cargar componentes intro = VideoFileClip(INTRO_VIDEO) outro = VideoFileClip(OUTRO_VIDEO) video_original = VideoFileClip(video_input) audio_original = video_original.audio # Duración del video editado (sin intro/outro) duracion_video = video_original.duration # Procesar audio audio_final = await procesar_audio( texto_tts, voz_seleccionada, duracion_video, audio_original ) # Redimensionar todos los clips a 1920x1080 target_width = 1920 target_height = 1080 # Redimensionar intro intro_resized = intro.resize((target_width, target_height)) # Redimensionar outro outro_resized = outro.resize((target_width, target_height)) # Redimensionar video principal video_resized = video_original.resize((target_width, target_height)) # Dividir el video en segmentos de 20 segundos y eliminar 2 segundos en cada corte segment_duration = 20 overlap = 2 # Segundos a eliminar en cada corte num_segments = int(duracion_video // (segment_duration - overlap)) + 1 segments = [] glitch_clips = [] glitch_sound = AudioFileClip(GLITCH_SOUND) start_time = 0 for i in range(num_segments): end_time = min(start_time + segment_duration, duracion_video) if start_time >= duracion_video: break # Extraer el segmento segment = video_resized.subclip(start_time, end_time) # Aplicar glitch al inicio del segmento (excepto el primero) if i > 0: glitch_segment = aplicar_glitch(segment.subclip(0, 0.5)) # Glitch de 0.5 segundos glitch_sound_clip = glitch_sound.set_start(start_time).volumex(0.5) glitch_clips.append(glitch_sound_clip) segment = concatenate_videoclips([glitch_segment, segment.subclip(0.5)], method="compose") segments.append(segment) # Avanzar al siguiente segmento, eliminando 2 segundos start_time += segment_duration - overlap # Combinar los segmentos procesados video_final = concatenate_videoclips(segments) # Combinar audio con efectos de glitch audio_final = CompositeAudioClip([audio_final] + glitch_clips).set_duration(video_final.duration) # Combinar video con audio video_con_audio = video_final.set_audio(audio_final) # Concatenar intro + video + outro SIN alteraciones video_final = concatenate_videoclips( [intro_resized, video_con_audio, outro_resized], method="compose", # Evitar problemas de grid padding=0 # Sin espacio entre clips ) # Renderizar video final con metadatos correctos with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp: video_final.write_videofile( tmp.name, codec="libx264", audio_codec="aac", fps=video_original.fps, # Mantener FPS original threads=4, ffmpeg_params=[ "-aspect", "16:9", # Forzar relación de aspecto "-vf", "scale=1920:1080" # Forzar escalado explícito ], verbose=False ) eliminar_archivo_tiempo(tmp.name) return tmp.name except Exception as e: logging.error(f" fallo general: {str(e)}") raise # Interfaz Gradio with gr.Blocks() as demo: gr.Markdown("# Editor de Video con IA") with gr.Tab("Principal"): video_input = gr.Video(label="Subir video") texto_tts = gr.Textbox( label="Texto para TTS", lines=3, placeholder="Escribe aquí tu texto..." ) voz_seleccionada = gr.Dropdown( label="Voz", choices=["es-ES-AlvaroNeural", "es-MX-BeatrizNeural"], value="es-ES-AlvaroNeural" ) procesar_btn = gr.Button("Generar Video") video_output = gr.Video(label="Video Procesado") with gr.Accordion("Ejemplos de Uso", open=False): gr.Examples( examples=[[EJEMPLO_VIDEO, "¡Hola! Esto es una prueba. Suscríbete al canal."]], inputs=[video_input, texto_tts], label="Ejemplos" ) procesar_btn.click( procesar_video, inputs=[video_input, texto_tts, voz_seleccionada], outputs=video_output ) if __name__ == "__main__": demo.queue().launch()