Spaces:
Sleeping
Sleeping
File size: 9,323 Bytes
4a04dae fc4d759 4a04dae 63e5b79 4a04dae fc4d759 4a04dae 63e5b79 4a04dae 63e5b79 4a04dae 63e5b79 4a04dae 63e5b79 4a04dae 63e5b79 4a04dae 63e5b79 4a04dae 63e5b79 4a04dae 63e5b79 4a04dae fc4d759 4a04dae fc4d759 4a04dae fc4d759 4a04dae 63e5b79 fc4d759 63e5b79 4a04dae 63e5b79 4a04dae fc4d759 63e5b79 fc4d759 63e5b79 fc4d759 63e5b79 4a04dae 63e5b79 fc4d759 4a04dae 63e5b79 4a04dae 63e5b79 fc4d759 4a04dae fc4d759 4a04dae fc4d759 63e5b79 fc4d759 4a04dae fc4d759 4a04dae fc4d759 4a04dae 5acea94 4a04dae 63e5b79 5acea94 4a04dae 5acea94 63e5b79 4a04dae 63e5b79 fc4d759 4a04dae 63e5b79 4a04dae fc4d759 4a04dae 63e5b79 fc4d759 63e5b79 4a04dae 511d2ce 4a04dae 5acea94 |
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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
import tempfile
import logging
import os
import asyncio
from moviepy.editor import *
import edge_tts
import gradio as gr
from pydub import AudioSegment
import psutil # Para monitoreo de recursos
# 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"
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}")
# Configuraci贸n de chunks
CHUNK_SIZE = 300 # 5 minutos por chunk (ajusta seg煤n tus recursos)
MAX_CHUNKS = 20 # L铆mite m谩ximo de chunks (previene loops infinitos)
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:
logging.info("Iniciando procesamiento de audio")
if not texto.strip():
raise ValueError("El texto para TTS no puede estar vac铆o.")
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:
communicate = edge_tts.Communicate(fragmento, voz)
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_tts:
await communicate.save(tmp_tts.name)
tts_audio = AudioFileClip(tmp_tts.name)
temp_files.append(tmp_tts.name)
audios_tts.append(tts_audio)
tts_audio_final = concatenate_audioclips(audios_tts)
if tts_audio_final.duration > duracion_video:
tts_audio_final = tts_audio_final.subclip(0, duracion_video)
needed_ms = int(duracion_video * 1000)
bg_music = AudioSegment.from_mp3(MUSIC_BG)
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)
audios = [bg_audio.set_duration(duracion_video)]
if audio_original:
audios.append(audio_original.volumex(0.7))
audios.append(tts_audio_final.volumex(0.85).set_start(0))
audio_final = CompositeAudioClip(audios).set_duration(duracion_video)
logging.info("Audio procesado exitosamente")
return audio_final
except Exception as e:
logging.error(f"Fallo en procesamiento de 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):
def glitch_effect(frame):
import numpy as np
frame = frame.copy()
height, width, _ = frame.shape
offset = np.random.randint(5, 15)
if height > 0:
frame[offset:, :] = np.roll(frame[:-offset, :], -offset, axis=0)
return frame
return video_clip.fl_image(glitch_effect)
async def procesar_fragmento(chunk, texto_tts, voz_seleccionada, start_time):
try:
audio_original = chunk.audio
duracion_chunk = chunk.duration
audio_final = await procesar_audio(
texto_tts,
voz_seleccionada,
duracion_chunk,
audio_original
)
segment_duration = 18
overlap = 2
total_segments = int((duracion_chunk) // (segment_duration)) + 1
segments = []
glitch_clips = []
glitch_sound = AudioFileClip(GLITCH_SOUND).volumex(0.5)
current_time = 0
for i in range(total_segments):
end_time = current_time + segment_duration + overlap
end_time = min(end_time, duracion_chunk)
full_segment = chunk.subclip(current_time, end_time)
if i > 0:
glitch_part = full_segment.subclip(0, 0.5)
glitch_part = aplicar_glitch(glitch_part)
processed_segment = concatenate_videoclips([
glitch_part,
full_segment.subclip(0.5)
], method="compose")
glitch_sound_clip = glitch_sound.set_start(start_time + current_time)
glitch_clips.append(glitch_sound_clip)
else:
processed_segment = full_segment
segments.append(processed_segment)
current_time += segment_duration
video_chunk = concatenate_videoclips(segments, method="compose")
video_chunk = video_chunk.set_audio(audio_final)
return video_chunk
except Exception as e:
logging.error(f"Fallo procesando fragmento: {str(e)}")
raise
async def procesar_video(video_input, texto_tts, voz_seleccionada):
try:
logging.info("Iniciando procesamiento de video")
video_original = VideoFileClip(video_input, target_resolution=(1080, 1920))
total_duration = video_original.duration
# Monitoreo de recursos
logging.info(f"Memoria inicial: {psutil.virtual_memory().percent}%")
logging.info(f"CPU inicial: {psutil.cpu_percent()}%")
# Dividir en chunks
chunks = []
for start in range(0, int(total_duration), CHUNK_SIZE):
end = min(start + CHUNK_SIZE, total_duration)
chunk = video_original.subclip(start, end)
chunks.append((start, chunk))
# Procesar cada chunk
processed_clips = []
for i, (start_time, chunk) in enumerate(chunks):
logging.info(f"Procesando chunk {i+1}/{len(chunks)}")
processed_chunk = await procesar_fragmento(chunk, texto_tts, voz_seleccionada, start_time)
processed_clips.append(processed_chunk)
# Combinar todos los chunks
final_video = concatenate_videoclips(processed_clips, method="compose")
# Agregar intro y outro
intro = VideoFileClip(INTRO_VIDEO, target_resolution=(1080, 1920))
outro = VideoFileClip(OUTRO_VIDEO, target_resolution=(1080, 1920))
final_video = concatenate_videoclips([intro, final_video, outro], method="compose")
# Renderizado final con optimizaci贸n
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp:
final_video.write_videofile(
tmp.name,
codec="libx264", # Cambiado de h264_nvenc a libx264 para compatibilidad
preset="ultrafast",
audio_codec="aac",
fps=video_original.fps,
threads=4,
bitrate="5M",
ffmpeg_params=[
"-crf", "23",
"-movflags", "+faststart",
"-vf", "scale=1920:1080"
],
verbose=False
)
eliminar_archivo_tiempo(tmp.name, 1800)
logging.info(f"Video final guardado: {tmp.name}")
return tmp.name
except Exception as e:
logging.error(f"Fallo general: {str(e)}")
raise
finally:
try:
video_original.close()
intro.close()
outro.close()
except:
pass
# 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() # Correcci贸n: Eliminados concurrency_count y max_size |