Spaces:
Sleeping
Sleeping
File size: 3,606 Bytes
43fcbe8 19224f2 8337d0b c9d2e08 07b3b3d dd712f9 07b3b3d 720c3d5 07b3b3d 1672ed1 07b3b3d 1672ed1 07b3b3d 19224f2 c7b9a72 07b3b3d 19224f2 8337d0b 07b3b3d 8337d0b 19224f2 c7b9a72 19224f2 8337d0b 07b3b3d 19224f2 8337d0b 07b3b3d 8337d0b fa201eb 1672ed1 720c3d5 07b3b3d c9d2e08 07b3b3d 518f669 07b3b3d d7f3a60 720c3d5 07b3b3d 19224f2 07b3b3d 8337d0b 720c3d5 07b3b3d 8337d0b 19224f2 518f669 9e5ee0a d7f3a60 07b3b3d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
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) |