import os import subprocess import gradio as gr from moviepy.editor import * import requests from datetime import datetime import tempfile # Configuración inicial PEXELS_API_KEY = os.getenv("PEXELS_API_KEY") # Configurar en variables de entorno # Lista de voces válidas VOICES = [ "es-MX-DaliaNeural", "es-ES-ElviraNeural", "es-AR-ElenaNeural", "en-US-JennyNeural", "fr-FR-DeniseNeural", "de-DE-KatjaNeural" ] def descargar_video(url, output_path): """Descarga un video y lo guarda localmente""" try: response = requests.get(url, stream=True, timeout=15) with open(output_path, 'wb') as f: for chunk in response.iter_content(chunk_size=1024*1024): f.write(chunk) return True except Exception as e: print(f"Error descargando video: {str(e)}") return False def generar_video(prompt, voz_seleccionada, musica=None): try: # 1. Generar voz voz_archivo = "voz.mp3" subprocess.run([ 'edge-tts', '--voice', voz_seleccionada, '--text', prompt, '--write-media', voz_archivo ], check=True) # 2. Buscar videos en Pexels headers = {"Authorization": PEXELS_API_KEY} response = requests.get( f"https://api.pexels.com/videos/search?query={prompt[:50]}&per_page=2", headers=headers, timeout=15 ) videos = response.json().get("videos", []) # 3. Descargar y procesar videos clips = [] for v in videos[:2]: video_url = v["video_files"][0]["link"] temp_video = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) if descargar_video(video_url, temp_video.name): clip = VideoFileClip(temp_video.name).subclip(0, min(5, VideoFileClip(temp_video.name).duration)) clips.append(clip) if not clips: raise Exception("No se pudieron cargar videos válidos") # 4. Procesar audio audio = AudioFileClip(voz_archivo) if musica: musica_clip = AudioFileClip(musica.name) if musica_clip.duration < audio.duration: musica_clip = musica_clip.loop(duration=audio.duration) audio = CompositeAudioClip([audio, musica_clip.volumex(0.3)]) # 5. Crear video final final_clip = concatenate_videoclips(clips).set_audio(audio) output_path = f"video_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4" final_clip.write_videofile(output_path, codec="libx264", audio_codec="aac", threads=2) return output_path except Exception as e: print(f"ERROR: {str(e)}") return None finally: # Limpieza if os.path.exists(voz_archivo): os.remove(voz_archivo) # Interfaz Gradio with gr.Blocks() as app: gr.Markdown("# 🎬 Generador de Videos Automático") with gr.Row(): with gr.Column(): prompt = gr.Textbox(label="Tema del video") voz = gr.Dropdown(label="Selección de voz", choices=VOICES, value="es-ES-ElviraNeural") musica = gr.File(label="Música de fondo (opcional)", file_types=[".mp3"]) btn = gr.Button("Generar Video") with gr.Column(): output = gr.Video(label="Resultado") btn.click( fn=generar_video, inputs=[prompt, voz, musica], outputs=output ) if __name__ == "__main__": app.launch(server_name="0.0.0.0", server_port=7860)