Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,12 +3,9 @@ import tempfile
|
|
3 |
import logging
|
4 |
import os
|
5 |
import asyncio
|
6 |
-
import time
|
7 |
-
from threading import Timer
|
8 |
from moviepy.editor import *
|
9 |
import edge_tts
|
10 |
import gradio as gr
|
11 |
-
from pydub import AudioSegment
|
12 |
|
13 |
# Configuraci贸n de Logs
|
14 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
@@ -16,13 +13,12 @@ logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(
|
|
16 |
# CONSTANTES DE ARCHIVOS
|
17 |
INTRO_VIDEO = "introvideo.mp4"
|
18 |
OUTRO_VIDEO = "outrovideo.mp4"
|
19 |
-
MUSIC_BG = "musicafondo.mp3"
|
20 |
FX_SOUND = "fxsound.mp3"
|
21 |
WATERMARK = "watermark.png"
|
22 |
EJEMPLO_VIDEO = "ejemplo.mp4"
|
23 |
|
24 |
# Validar existencia de archivos
|
25 |
-
for file in [INTRO_VIDEO, OUTRO_VIDEO,
|
26 |
if not os.path.exists(file):
|
27 |
logging.error(f"Falta archivo necesario: {file}")
|
28 |
raise FileNotFoundError(f"Falta: {file}")
|
@@ -46,8 +42,8 @@ def validar_texto(texto):
|
|
46 |
if any(c in texto_limpio for c in ["|", "\n", "\r"]):
|
47 |
raise gr.Error("鈿狅笍 Caracteres no permitidos detectados")
|
48 |
|
49 |
-
async def procesar_audio(texto, voz,
|
50 |
-
"""Genera y
|
51 |
temp_files = []
|
52 |
try:
|
53 |
validar_texto(texto)
|
@@ -59,31 +55,11 @@ async def procesar_audio(texto, voz, duracion_total, duracion_intro, max_tts_tim
|
|
59 |
tts_audio = AudioFileClip(tmp_tts.name)
|
60 |
temp_files.append(tmp_tts.name)
|
61 |
|
62 |
-
#
|
63 |
-
if tts_audio.duration >
|
64 |
-
tts_audio = tts_audio.subclip(0,
|
65 |
|
66 |
-
|
67 |
-
bg_music = AudioSegment.from_mp3(MUSIC_BG)
|
68 |
-
needed_ms = int(duracion_total * 1000)
|
69 |
-
repeticiones = needed_ms // len(bg_music) + 1
|
70 |
-
bg_music = bg_music * repeticiones
|
71 |
-
bg_music = bg_music[:needed_ms].fade_out(5000)
|
72 |
-
|
73 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_bg:
|
74 |
-
bg_music.export(tmp_bg.name, format="mp3")
|
75 |
-
bg_audio = AudioFileClip(tmp_bg.name).volumex(0.15)
|
76 |
-
temp_files.append(tmp_bg.name)
|
77 |
-
|
78 |
-
# Combinar audios con duraciones exactas
|
79 |
-
audio_final = CompositeAudioClip([
|
80 |
-
bg_audio.set_duration(duracion_total),
|
81 |
-
tts_audio.volumex(0.85)
|
82 |
-
.set_start(duracion_intro)
|
83 |
-
.set_duration(max_tts_time)
|
84 |
-
]).set_duration(duracion_total)
|
85 |
-
|
86 |
-
return audio_final
|
87 |
|
88 |
except Exception as e:
|
89 |
logging.error(f" fallo en audio: {str(e)}")
|
@@ -144,23 +120,17 @@ async def procesar_video(video_input, texto_tts, voz_seleccionada, metodo_corte,
|
|
144 |
video_final = concatenate_videoclips([intro, video_editado, outro])
|
145 |
duracion_total = video_final.duration
|
146 |
|
147 |
-
#
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
intro.duration,
|
153 |
-
video_editado_duration
|
154 |
-
)
|
155 |
|
156 |
-
# Combinar
|
157 |
-
audios = [
|
158 |
if audio_original:
|
159 |
-
audios.append(
|
160 |
-
|
161 |
-
.set_duration(video_editado_duration)
|
162 |
-
.set_start(intro.duration)
|
163 |
-
)
|
164 |
|
165 |
audio_final = CompositeAudioClip(audios).set_duration(duracion_total)
|
166 |
|
|
|
3 |
import logging
|
4 |
import os
|
5 |
import asyncio
|
|
|
|
|
6 |
from moviepy.editor import *
|
7 |
import edge_tts
|
8 |
import gradio as gr
|
|
|
9 |
|
10 |
# Configuraci贸n de Logs
|
11 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
|
|
13 |
# CONSTANTES DE ARCHIVOS
|
14 |
INTRO_VIDEO = "introvideo.mp4"
|
15 |
OUTRO_VIDEO = "outrovideo.mp4"
|
|
|
16 |
FX_SOUND = "fxsound.mp3"
|
17 |
WATERMARK = "watermark.png"
|
18 |
EJEMPLO_VIDEO = "ejemplo.mp4"
|
19 |
|
20 |
# Validar existencia de archivos
|
21 |
+
for file in [INTRO_VIDEO, OUTRO_VIDEO, FX_SOUND, WATERMARK, EJEMPLO_VIDEO]:
|
22 |
if not os.path.exists(file):
|
23 |
logging.error(f"Falta archivo necesario: {file}")
|
24 |
raise FileNotFoundError(f"Falta: {file}")
|
|
|
42 |
if any(c in texto_limpio for c in ["|", "\n", "\r"]):
|
43 |
raise gr.Error("鈿狅笍 Caracteres no permitidos detectados")
|
44 |
|
45 |
+
async def procesar_audio(texto, voz, duracion_maxima):
|
46 |
+
"""Genera TTS y lo limita a la duraci贸n m谩xima disponible"""
|
47 |
temp_files = []
|
48 |
try:
|
49 |
validar_texto(texto)
|
|
|
55 |
tts_audio = AudioFileClip(tmp_tts.name)
|
56 |
temp_files.append(tmp_tts.name)
|
57 |
|
58 |
+
# Limitar TTS a la duraci贸n m谩xima
|
59 |
+
if tts_audio.duration > duracion_maxima:
|
60 |
+
tts_audio = tts_audio.subclip(0, duracion_maxima)
|
61 |
|
62 |
+
return tts_audio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
except Exception as e:
|
65 |
logging.error(f" fallo en audio: {str(e)}")
|
|
|
120 |
video_final = concatenate_videoclips([intro, video_editado, outro])
|
121 |
duracion_total = video_final.duration
|
122 |
|
123 |
+
# Duraci贸n m谩xima para el TTS (despu茅s de la intro)
|
124 |
+
max_tts_time = video_editado_duration
|
125 |
+
|
126 |
+
# Procesar TTS
|
127 |
+
tts_audio = await procesar_audio(texto_tts, voz_seleccionada, max_tts_time)
|
|
|
|
|
|
|
128 |
|
129 |
+
# Combinar audios
|
130 |
+
audios = []
|
131 |
if audio_original:
|
132 |
+
audios.append(audio_original.set_duration(video_editado_duration))
|
133 |
+
audios.append(tts_audio.set_start(intro.duration))
|
|
|
|
|
|
|
134 |
|
135 |
audio_final = CompositeAudioClip(audios).set_duration(duracion_total)
|
136 |
|