Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -69,9 +69,9 @@ async def procesar_audio(texto, voz, duracion_video, audio_original):
|
|
| 69 |
if tts_audio_final.duration > duracion_video:
|
| 70 |
tts_audio_final = tts_audio_final.subclip(0, duracion_video)
|
| 71 |
|
| 72 |
-
# Preparar m煤sica de fondo en loop
|
| 73 |
-
bg_music = AudioSegment.from_mp3(MUSIC_BG)
|
| 74 |
needed_ms = int(duracion_video * 1000)
|
|
|
|
| 75 |
repeticiones = needed_ms // len(bg_music) + 1
|
| 76 |
bg_music = bg_music * repeticiones
|
| 77 |
bg_music = bg_music[:needed_ms].fade_out(1000)
|
|
@@ -101,16 +101,17 @@ async def procesar_audio(texto, voz, duracion_video, audio_original):
|
|
| 101 |
logging.warning(f"Error limpiando {file}: {e}")
|
| 102 |
|
| 103 |
def aplicar_glitch(video_clip):
|
| 104 |
-
"""Aplica un efecto de glitch al video
|
| 105 |
def glitch_effect(frame):
|
| 106 |
import numpy as np
|
| 107 |
height, width, _ = frame.shape
|
| 108 |
-
offset = np.random.randint(-
|
|
|
|
| 109 |
if offset > 0:
|
| 110 |
offset = min(offset, height)
|
| 111 |
if offset < 0:
|
| 112 |
offset = max(offset, -height + 1)
|
| 113 |
-
if
|
| 114 |
frame[offset:, :] = np.roll(frame[:-offset, :], -offset, axis=0)
|
| 115 |
return frame
|
| 116 |
|
|
@@ -118,10 +119,10 @@ def aplicar_glitch(video_clip):
|
|
| 118 |
|
| 119 |
async def procesar_video(video_input, texto_tts, voz_seleccionada):
|
| 120 |
try:
|
| 121 |
-
# Cargar componentes
|
| 122 |
-
intro = VideoFileClip(INTRO_VIDEO)
|
| 123 |
-
outro = VideoFileClip(OUTRO_VIDEO)
|
| 124 |
-
video_original = VideoFileClip(video_input)
|
| 125 |
audio_original = video_original.audio
|
| 126 |
|
| 127 |
# Duraci贸n del video editado (sin intro/outro)
|
|
@@ -135,26 +136,13 @@ async def procesar_video(video_input, texto_tts, voz_seleccionada):
|
|
| 135 |
audio_original
|
| 136 |
)
|
| 137 |
|
| 138 |
-
# Redimensionar todos los clips a 1920x1080
|
| 139 |
-
target_width = 1920
|
| 140 |
-
target_height = 1080
|
| 141 |
-
|
| 142 |
-
# Redimensionar intro
|
| 143 |
-
intro_resized = intro.resize((target_width, target_height))
|
| 144 |
-
|
| 145 |
-
# Redimensionar outro
|
| 146 |
-
outro_resized = outro.resize((target_width, target_height))
|
| 147 |
-
|
| 148 |
-
# Redimensionar video principal
|
| 149 |
-
video_resized = video_original.resize((target_width, target_height))
|
| 150 |
-
|
| 151 |
# Dividir el video en segmentos de 20 segundos y eliminar 2 segundos en cada corte
|
| 152 |
segment_duration = 20
|
| 153 |
overlap = 2 # Segundos a eliminar en cada corte
|
| 154 |
num_segments = int(duracion_video // (segment_duration - overlap)) + 1
|
| 155 |
segments = []
|
| 156 |
glitch_clips = []
|
| 157 |
-
glitch_sound = AudioFileClip(GLITCH_SOUND)
|
| 158 |
|
| 159 |
start_time = 0
|
| 160 |
for i in range(num_segments):
|
|
@@ -163,14 +151,14 @@ async def procesar_video(video_input, texto_tts, voz_seleccionada):
|
|
| 163 |
break
|
| 164 |
|
| 165 |
# Extraer el segmento
|
| 166 |
-
segment =
|
| 167 |
|
| 168 |
-
# Aplicar glitch
|
| 169 |
-
if i > 0:
|
| 170 |
-
glitch_segment = aplicar_glitch(segment.subclip(0, 0.
|
| 171 |
-
glitch_sound_clip = glitch_sound.set_start(start_time)
|
| 172 |
glitch_clips.append(glitch_sound_clip)
|
| 173 |
-
segment = concatenate_videoclips([glitch_segment, segment.subclip(0.
|
| 174 |
|
| 175 |
segments.append(segment)
|
| 176 |
|
|
@@ -186,24 +174,28 @@ async def procesar_video(video_input, texto_tts, voz_seleccionada):
|
|
| 186 |
# Combinar video con audio
|
| 187 |
video_con_audio = video_final.set_audio(audio_final)
|
| 188 |
|
| 189 |
-
# Concatenar intro + video + outro
|
| 190 |
video_final = concatenate_videoclips(
|
| 191 |
-
[
|
| 192 |
-
method="compose",
|
| 193 |
-
padding=0
|
| 194 |
)
|
| 195 |
|
| 196 |
-
# Renderizar video final con
|
| 197 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp:
|
| 198 |
video_final.write_videofile(
|
| 199 |
tmp.name,
|
| 200 |
codec="libx264",
|
| 201 |
audio_codec="aac",
|
| 202 |
-
fps=video_original.fps,
|
| 203 |
threads=4,
|
|
|
|
| 204 |
ffmpeg_params=[
|
| 205 |
-
"-
|
| 206 |
-
"-
|
|
|
|
|
|
|
|
|
|
| 207 |
],
|
| 208 |
verbose=False
|
| 209 |
)
|
|
|
|
| 69 |
if tts_audio_final.duration > duracion_video:
|
| 70 |
tts_audio_final = tts_audio_final.subclip(0, duracion_video)
|
| 71 |
|
| 72 |
+
# Preparar m煤sica de fondo en loop (OPTIMIZADO)
|
|
|
|
| 73 |
needed_ms = int(duracion_video * 1000)
|
| 74 |
+
bg_music = AudioSegment.from_mp3(MUSIC_BG)
|
| 75 |
repeticiones = needed_ms // len(bg_music) + 1
|
| 76 |
bg_music = bg_music * repeticiones
|
| 77 |
bg_music = bg_music[:needed_ms].fade_out(1000)
|
|
|
|
| 101 |
logging.warning(f"Error limpiando {file}: {e}")
|
| 102 |
|
| 103 |
def aplicar_glitch(video_clip):
|
| 104 |
+
"""Aplica un efecto de glitch al video (OPTIMIZADO)"""
|
| 105 |
def glitch_effect(frame):
|
| 106 |
import numpy as np
|
| 107 |
height, width, _ = frame.shape
|
| 108 |
+
offset = np.random.randint(-5, 5) # Reducido el rango de glitch
|
| 109 |
+
if offset == 0: return frame # Evitar operaciones innecesarias
|
| 110 |
if offset > 0:
|
| 111 |
offset = min(offset, height)
|
| 112 |
if offset < 0:
|
| 113 |
offset = max(offset, -height + 1)
|
| 114 |
+
if height > 0:
|
| 115 |
frame[offset:, :] = np.roll(frame[:-offset, :], -offset, axis=0)
|
| 116 |
return frame
|
| 117 |
|
|
|
|
| 119 |
|
| 120 |
async def procesar_video(video_input, texto_tts, voz_seleccionada):
|
| 121 |
try:
|
| 122 |
+
# Cargar componentes con OPTIMIZACI脫N DE MEMORIA
|
| 123 |
+
intro = VideoFileClip(INTRO_VIDEO, target_resolution=(1080, 1920))
|
| 124 |
+
outro = VideoFileClip(OUTRO_VIDEO, target_resolution=(1080, 1920))
|
| 125 |
+
video_original = VideoFileClip(video_input, target_resolution=(1080, 1920))
|
| 126 |
audio_original = video_original.audio
|
| 127 |
|
| 128 |
# Duraci贸n del video editado (sin intro/outro)
|
|
|
|
| 136 |
audio_original
|
| 137 |
)
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
# Dividir el video en segmentos de 20 segundos y eliminar 2 segundos en cada corte
|
| 140 |
segment_duration = 20
|
| 141 |
overlap = 2 # Segundos a eliminar en cada corte
|
| 142 |
num_segments = int(duracion_video // (segment_duration - overlap)) + 1
|
| 143 |
segments = []
|
| 144 |
glitch_clips = []
|
| 145 |
+
glitch_sound = AudioFileClip(GLITCH_SOUND).volumex(0.5) # Pre-cargado una vez
|
| 146 |
|
| 147 |
start_time = 0
|
| 148 |
for i in range(num_segments):
|
|
|
|
| 151 |
break
|
| 152 |
|
| 153 |
# Extraer el segmento
|
| 154 |
+
segment = video_original.subclip(start_time, end_time)
|
| 155 |
|
| 156 |
+
# Aplicar glitch cada 2 segmentos (40 segundos) en lugar de cada 1
|
| 157 |
+
if i % 2 == 0 and i > 0:
|
| 158 |
+
glitch_segment = aplicar_glitch(segment.subclip(0, 0.3)) # Duraci贸n reducida
|
| 159 |
+
glitch_sound_clip = glitch_sound.set_start(start_time)
|
| 160 |
glitch_clips.append(glitch_sound_clip)
|
| 161 |
+
segment = concatenate_videoclips([glitch_segment, segment.subclip(0.3)], method="compose")
|
| 162 |
|
| 163 |
segments.append(segment)
|
| 164 |
|
|
|
|
| 174 |
# Combinar video con audio
|
| 175 |
video_con_audio = video_final.set_audio(audio_final)
|
| 176 |
|
| 177 |
+
# Concatenar intro + video + outro
|
| 178 |
video_final = concatenate_videoclips(
|
| 179 |
+
[intro, video_con_audio, outro],
|
| 180 |
+
method="compose",
|
| 181 |
+
padding=0
|
| 182 |
)
|
| 183 |
|
| 184 |
+
# Renderizar video final con OPTIMIZACIONES DE CODIFICACI脫N
|
| 185 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp:
|
| 186 |
video_final.write_videofile(
|
| 187 |
tmp.name,
|
| 188 |
codec="libx264",
|
| 189 |
audio_codec="aac",
|
| 190 |
+
fps=video_original.fps,
|
| 191 |
threads=4,
|
| 192 |
+
bitrate="5M", # Control de tama帽o
|
| 193 |
ffmpeg_params=[
|
| 194 |
+
"-preset", "ultrafast", # Velocidad de codificaci贸n
|
| 195 |
+
"-crf", "23", # Calidad est谩ndar
|
| 196 |
+
"-movflags", "+faststart", # Para streaming
|
| 197 |
+
"-aspect", "16:9",
|
| 198 |
+
"-vf", "scale=1920:1080"
|
| 199 |
],
|
| 200 |
verbose=False
|
| 201 |
)
|