Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,10 +14,11 @@ logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(
|
|
| 14 |
INTRO_VIDEO = "introvideo.mp4"
|
| 15 |
OUTRO_VIDEO = "outrovideo.mp4"
|
| 16 |
MUSIC_BG = "musicafondo.mp3"
|
|
|
|
| 17 |
EJEMPLO_VIDEO = "ejemplo.mp4"
|
| 18 |
|
| 19 |
# Validar existencia de archivos
|
| 20 |
-
for file in [INTRO_VIDEO, OUTRO_VIDEO, MUSIC_BG, EJEMPLO_VIDEO]:
|
| 21 |
if not os.path.exists(file):
|
| 22 |
logging.error(f"Falta archivo necesario: {file}")
|
| 23 |
raise FileNotFoundError(f"Falta: {file}")
|
|
@@ -33,45 +34,37 @@ def eliminar_archivo_tiempo(ruta, delay=1800):
|
|
| 33 |
from threading import Timer
|
| 34 |
Timer(delay, eliminar).start()
|
| 35 |
|
| 36 |
-
def
|
| 37 |
-
texto_limpio = texto.strip()
|
| 38 |
-
if len(texto_limpio) < 3:
|
| 39 |
-
raise gr.Error("鈿狅笍 El texto debe tener al menos 3 caracteres")
|
| 40 |
-
if any(c in texto_limpio for c in ["|", "\n", "\r"]):
|
| 41 |
-
raise gr.Error("鈿狅笍 Caracteres no permitidos detectados")
|
| 42 |
-
|
| 43 |
-
async def procesar_audio(texto, voz, duracion_video_editado, duracion_intro):
|
| 44 |
temp_files = []
|
| 45 |
try:
|
| 46 |
-
|
| 47 |
communicate = edge_tts.Communicate(texto, voz)
|
| 48 |
-
|
| 49 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_tts:
|
| 50 |
await communicate.save(tmp_tts.name)
|
| 51 |
tts_audio = AudioFileClip(tmp_tts.name)
|
| 52 |
temp_files.append(tmp_tts.name)
|
| 53 |
|
| 54 |
-
# Limitar TTS al
|
| 55 |
-
if tts_audio.duration >
|
| 56 |
-
tts_audio = tts_audio.subclip(0,
|
| 57 |
|
| 58 |
# Preparar m煤sica de fondo en loop
|
| 59 |
bg_music = AudioSegment.from_mp3(MUSIC_BG)
|
| 60 |
-
needed_ms = int(
|
| 61 |
repeticiones = needed_ms // len(bg_music) + 1
|
| 62 |
bg_music = bg_music * repeticiones
|
| 63 |
-
bg_music = bg_music[:needed_ms].fade_out(
|
| 64 |
|
| 65 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_bg:
|
| 66 |
bg_music.export(tmp_bg.name, format="mp3")
|
| 67 |
-
bg_audio = AudioFileClip(tmp_bg.name).volumex(0.
|
| 68 |
temp_files.append(tmp_bg.name)
|
| 69 |
|
| 70 |
# Combinar audios
|
| 71 |
audio_final = CompositeAudioClip([
|
| 72 |
-
bg_audio.set_duration(
|
| 73 |
-
tts_audio.volumex(0.
|
| 74 |
-
]).set_duration(
|
| 75 |
|
| 76 |
return audio_final
|
| 77 |
|
|
@@ -85,6 +78,18 @@ async def procesar_audio(texto, voz, duracion_video_editado, duracion_intro):
|
|
| 85 |
except Exception as e:
|
| 86 |
logging.warning(f"Error limpiando {file}: {e}")
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
async def procesar_video(video_input, texto_tts, voz_seleccionada):
|
| 89 |
try:
|
| 90 |
# Cargar componentes
|
|
@@ -92,25 +97,19 @@ async def procesar_video(video_input, texto_tts, voz_seleccionada):
|
|
| 92 |
outro = VideoFileClip(OUTRO_VIDEO)
|
| 93 |
video_original = VideoFileClip(video_input)
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
|
| 97 |
-
duracion_video_editado = video_original.duration
|
| 98 |
|
| 99 |
# Procesar audio
|
| 100 |
-
audio_final = await procesar_audio(
|
| 101 |
-
texto_tts,
|
| 102 |
-
voz_seleccionada,
|
| 103 |
-
duracion_video_editado,
|
| 104 |
-
duracion_intro
|
| 105 |
-
)
|
| 106 |
|
| 107 |
-
#
|
| 108 |
-
|
| 109 |
|
| 110 |
-
#
|
| 111 |
video_final = concatenate_videoclips(
|
| 112 |
-
[intro,
|
| 113 |
-
method="chain"
|
| 114 |
)
|
| 115 |
|
| 116 |
# Renderizar video final
|
|
|
|
| 14 |
INTRO_VIDEO = "introvideo.mp4"
|
| 15 |
OUTRO_VIDEO = "outrovideo.mp4"
|
| 16 |
MUSIC_BG = "musicafondo.mp3"
|
| 17 |
+
FX_GLITCH = "glitch.mp4" # Video con efecto glitch de 0.5 segundos
|
| 18 |
EJEMPLO_VIDEO = "ejemplo.mp4"
|
| 19 |
|
| 20 |
# Validar existencia de archivos
|
| 21 |
+
for file in [INTRO_VIDEO, OUTRO_VIDEO, MUSIC_BG, FX_GLITCH, EJEMPLO_VIDEO]:
|
| 22 |
if not os.path.exists(file):
|
| 23 |
logging.error(f"Falta archivo necesario: {file}")
|
| 24 |
raise FileNotFoundError(f"Falta: {file}")
|
|
|
|
| 34 |
from threading import Timer
|
| 35 |
Timer(delay, eliminar).start()
|
| 36 |
|
| 37 |
+
async def procesar_audio(texto, voz, duracion_video):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
temp_files = []
|
| 39 |
try:
|
| 40 |
+
# Generar TTS
|
| 41 |
communicate = edge_tts.Communicate(texto, voz)
|
|
|
|
| 42 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_tts:
|
| 43 |
await communicate.save(tmp_tts.name)
|
| 44 |
tts_audio = AudioFileClip(tmp_tts.name)
|
| 45 |
temp_files.append(tmp_tts.name)
|
| 46 |
|
| 47 |
+
# Limitar TTS al video
|
| 48 |
+
if tts_audio.duration > duracion_video:
|
| 49 |
+
tts_audio = tts_audio.subclip(0, duracion_video)
|
| 50 |
|
| 51 |
# Preparar m煤sica de fondo en loop
|
| 52 |
bg_music = AudioSegment.from_mp3(MUSIC_BG)
|
| 53 |
+
needed_ms = int(duracion_video * 1000)
|
| 54 |
repeticiones = needed_ms // len(bg_music) + 1
|
| 55 |
bg_music = bg_music * repeticiones
|
| 56 |
+
bg_music = bg_music[:needed_ms].fade_out(1000)
|
| 57 |
|
| 58 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_bg:
|
| 59 |
bg_music.export(tmp_bg.name, format="mp3")
|
| 60 |
+
bg_audio = AudioFileClip(tmp_bg.name).volumex(0.2)
|
| 61 |
temp_files.append(tmp_bg.name)
|
| 62 |
|
| 63 |
# Combinar audios
|
| 64 |
audio_final = CompositeAudioClip([
|
| 65 |
+
bg_audio.set_duration(duracion_video),
|
| 66 |
+
tts_audio.volumex(0.8).set_start(0)
|
| 67 |
+
]).set_duration(duracion_video)
|
| 68 |
|
| 69 |
return audio_final
|
| 70 |
|
|
|
|
| 78 |
except Exception as e:
|
| 79 |
logging.warning(f"Error limpiando {file}: {e}")
|
| 80 |
|
| 81 |
+
def agregar_glitch(video, intervalo=40):
|
| 82 |
+
"""Agrega glitch cada X segundos sin alterar el video"""
|
| 83 |
+
duracion = video.duration
|
| 84 |
+
glitch = VideoFileClip(FX_GLITCH).set_duration(0.5)
|
| 85 |
+
|
| 86 |
+
# Crear clips de glitch en los intervalos
|
| 87 |
+
glitches = []
|
| 88 |
+
for t in range(intervalo, math.ceil(duracion), intervalo):
|
| 89 |
+
glitches.append(glitch.set_start(t).set_pos("center"))
|
| 90 |
+
|
| 91 |
+
return CompositeVideoClip([video] + glitches)
|
| 92 |
+
|
| 93 |
async def procesar_video(video_input, texto_tts, voz_seleccionada):
|
| 94 |
try:
|
| 95 |
# Cargar componentes
|
|
|
|
| 97 |
outro = VideoFileClip(OUTRO_VIDEO)
|
| 98 |
video_original = VideoFileClip(video_input)
|
| 99 |
|
| 100 |
+
# Duraci贸n total del video editado
|
| 101 |
+
duracion_video = video_original.duration
|
|
|
|
| 102 |
|
| 103 |
# Procesar audio
|
| 104 |
+
audio_final = await procesar_audio(texto_tts, voz_seleccionada, duracion_video)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
# Agregar glitch al video original
|
| 107 |
+
video_con_glitch = agregar_glitch(video_original, intervalo=40)
|
| 108 |
|
| 109 |
+
# Combinar con intro/outro
|
| 110 |
video_final = concatenate_videoclips(
|
| 111 |
+
[intro, video_con_glitch, outro],
|
| 112 |
+
method="chain"
|
| 113 |
)
|
| 114 |
|
| 115 |
# Renderizar video final
|