File size: 8,450 Bytes
8b274aa
15e8c2d
bafc5cd
15e8c2d
b82e6a6
 
 
 
15e8c2d
 
6d66777
b82e6a6
 
 
 
 
 
 
15e8c2d
b82e6a6
 
1829fd6
b82e6a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1829fd6
b82e6a6
 
9b7097e
b82e6a6
 
 
 
 
 
 
 
1829fd6
b82e6a6
 
8b274aa
b82e6a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d66777
b82e6a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374c72e
15e8c2d
b82e6a6
 
 
 
 
 
 
 
 
 
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import os
import asyncio
import logging
import tempfile
import time
import shutil
from datetime import datetime, timedelta
from moviepy.editor import VideoFileClip, AudioFileClip, CompositeAudioClip, ColorClip, TextClip, CompositeVideoClip
import edge_tts
import gradio as gr

# Configuración avanzada de logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler("video_generator.log"),
        logging.StreamHandler()
    ]
)
logger = logging.getLogger(__name__)

# Directorio temporal personalizado con limpieza automática
TEMP_DIR = "temp_media"
os.makedirs(TEMP_DIR, exist_ok=True)

def clean_old_files():
    """Elimina archivos temporales con más de 24 horas"""
    now = time.time()
    cutoff = now - (24 * 3600)
    
    for filename in os.listdir(TEMP_DIR):
        file_path = os.path.join(TEMP_DIR, filename)
        if os.path.isfile(file_path):
            file_time = os.path.getmtime(file_path)
            if file_time < cutoff:
                try:
                    os.remove(file_path)
                    logger.info(f"Eliminado archivo antiguo: {filename}")
                except Exception as e:
                    logger.error(f"Error al eliminar {filename}: {e}")

async def text_to_speech(text, voice="es-ES-ElviraNeural"):
    """Convierte texto a voz y guarda en archivo temporal"""
    clean_old_files()
    output_path = os.path.join(TEMP_DIR, f"tts_{datetime.now().strftime('%Y%m%d%H%M%S')}.mp3")
    
    try:
        logger.info(f"Generando TTS para texto de {len(text)} caracteres")
        communicate = edge_tts.Communicate(text, voice)
        await communicate.save(output_path)
        return output_path
    except Exception as e:
        logger.error(f"Error en TTS: {e}")
        raise

def create_audio_loop(audio_path, target_duration):
    """Crea un loop de audio hasta alcanzar la duración objetivo"""
    try:
        audio = AudioFileClip(audio_path)
        if audio.duration >= target_duration:
            return audio.subclip(0, target_duration)
        
        loops_needed = int(target_duration // audio.duration) + 1
        clips = [audio] * loops_needed
        looped_audio = concatenate_audioclips(clips).subclip(0, target_duration)
        return looped_audio
    except Exception as e:
        logger.error(f"Error al crear loop de audio: {e}")
        raise

def create_video_with_text(text, duration, size=(1280, 720)):
    """Crea un video simple con texto centrado"""
    try:
        # Fondo del video
        bg_clip = ColorClip(size, color=(30, 30, 30), duration=duration)
        
        # Texto con ajuste automático de tamaño
        text_clip = TextClip(
            text,
            fontsize=28,
            color='white',
            font='Arial-Bold',
            size=(size[0]-100, size[1]-100),
            method='caption',
            align='center'
        ).set_position('center').set_duration(duration)
        
        return CompositeVideoClip([bg_clip, text_clip])
    except Exception as e:
        logger.error(f"Error al crear video con texto: {e}")
        raise

async def generate_video_content(text, background_music=None, use_tts=True):
    """Función principal que genera el contenido del video"""
    try:
        clean_old_files()
        
        # 1. Procesar audio principal
        if use_tts:
            voice_path = await text_to_speech(text)
            main_audio = AudioFileClip(voice_path)
        else:
            # Si no usamos TTS, creamos un audio silencioso de la duración estimada
            estimated_duration = max(5, len(text.split()) / 3)  # Estimación basada en palabras
            main_audio = AudioFileClip(lambda t: 0, duration=estimated_duration)
        
        duration = main_audio.duration
        
        # 2. Procesar música de fondo
        final_audio = main_audio
        if background_music:
            try:
                bg_music_loop = create_audio_loop(background_music, duration).volumex(0.2)
                final_audio = CompositeAudioClip([bg_music_loop, main_audio])
            except Exception as e:
                logger.error(f"Error al procesar música de fondo, continuando sin ella: {e}")
        
        # 3. Crear video
        video_clip = create_video_with_text(text, duration)
        video_clip = video_clip.set_audio(final_audio)
        
        # 4. Guardar resultado
        output_path = os.path.join(TEMP_DIR, f"video_{datetime.now().strftime('%Y%m%d%H%M%S')}.mp4")
        video_clip.write_videofile(
            output_path,
            fps=24,
            threads=4,
            codec='libx264',
            audio_codec='aac',
            preset='fast',
            logger=None
        )
        
        return output_path
    except Exception as e:
        logger.error(f"Error en generate_video_content: {e}")
        raise
    finally:
        # Cerrar todos los clips para liberar recursos
        if 'main_audio' in locals():
            main_audio.close()
        if 'bg_music_loop' in locals():
            bg_music_loop.close()
        if 'video_clip' in locals():
            video_clip.close()

# Interfaz Gradio mejorada
with gr.Blocks(title="Generador de Videos Avanzado", theme="soft") as app:
    gr.Markdown("""
    # 🎥 Generador de Videos Automático
    Crea videos con voz sintetizada y música de fondo
    """)
    
    with gr.Tab("Configuración Principal"):
        with gr.Row():
            with gr.Column():
                text_input = gr.Textbox(
                    label="Texto del Video",
                    placeholder="Escribe aquí el contenido de tu video...",
                    lines=5,
                    max_lines=20
                )
                
                with gr.Accordion("Opciones Avanzadas", open=False):
                    use_tts = gr.Checkbox(
                        label="Usar Texto a Voz (TTS)",
                        value=True
                    )
                    voice_selector = gr.Dropdown(
                        label="Voz TTS",
                        choices=["es-ES-ElviraNeural", "es-MX-DaliaNeural", "es-US-AlonsoNeural"],
                        value="es-ES-ElviraNeural",
                        visible=True
                    )
                    
                    bg_music = gr.Audio(
                        label="Música de Fondo",
                        type="filepath",
                        sources=["upload"],
                        format="mp3"
                    )
                
                generate_btn = gr.Button("Generar Video", variant="primary")
            
            with gr.Column():
                video_output = gr.Video(
                    label="Video Resultante",
                    format="mp4",
                    interactive=False
                )
                status_output = gr.Textbox(
                    label="Estado",
                    interactive=False
                )
    
    # Lógica para mostrar/ocultar selector de voz
    use_tts.change(
        fn=lambda x: gr.Dropdown(visible=x),
        inputs=use_tts,
        outputs=voice_selector
    )
    
    # Función principal de generación
    def generate_video(text, use_tts, voice, bg_music):
        try:
            if not text.strip():
                raise ValueError("Por favor ingresa un texto para el video")
            
            # Limpieza inicial
            clean_old_files()
            
            # Generación del video
            video_path = asyncio.run(
                generate_video_content(
                    text=text,
                    use_tts=use_tts,
                    background_music=bg_music
                )
            )
            
            return video_path, "✅ Video generado con éxito"
        except Exception as e:
            logger.error(f"Error en la generación: {str(e)}")
            return None, f"❌ Error: {str(e)}"
    
    generate_btn.click(
        fn=generate_video,
        inputs=[text_input, use_tts, voice_selector, bg_music],
        outputs=[video_output, status_output]
    )

if __name__ == "__main__":
    # Limpieza inicial de archivos antiguos
    clean_old_files()
    
    # Configuración del servidor
    app.launch(
        server_name="0.0.0.0",
        server_port=7860,
        show_error=True,
        share=False,
        favicon_path=None
    )