gnosticdev commited on
Commit
5da1288
·
verified ·
1 Parent(s): 5d96110

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -51
app.py CHANGED
@@ -37,64 +37,46 @@ def eliminar_archivo_tiempo(ruta, delay=1800):
37
  from threading import Timer
38
  Timer(delay, eliminar).start()
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  async def generar_tts(texto, voz, duracion_total):
41
  try:
42
- # Validar texto
43
  if not texto.strip():
44
  raise ValueError("El texto para TTS no puede estar vacío.")
45
- if len(texto) > 1000: # Límite aproximado de Edge TTS
46
- texto = texto[:1000] # Truncar texto si es demasiado largo
47
 
48
- # Validar voz
49
  voces_validas = [
50
  "es-ES-AlvaroNeural", "es-MX-BeatrizNeural",
51
  "es-ES-ElviraNeural", "es-MX-JavierNeural",
52
- "es-AR-ElenaNeural", "es-AR-TomasNeural",
53
- "es-CL-CatalinaNeural", "es-CL-LorenzoNeural",
54
- "es-CO-SofiaNeural", "es-CO-GonzaloNeural",
55
- "es-PE-CamilaNeural", "es-PE-AlexNeural",
56
- "es-VE-MariaNeural", "es-VE-ManuelNeural",
57
- "es-US-AlonsoNeural", "es-US-PalomaNeural",
58
- "es-ES-AbrilNeural", "es-ES-DarioNeural",
59
- "es-ES-HelenaRUS", "es-ES-LauraNeural",
60
- "es-ES-PabloNeural", "es-ES-TriniNeural",
61
- "en-US-AriaNeural", "en-US-GuyNeural",
62
- "en-US-JennyNeural", "en-US-AmberNeural",
63
- "en-US-AnaNeural", "en-US-AshleyNeural",
64
- "en-US-BrandonNeural", "en-US-ChristopherNeural",
65
- "en-US-CoraNeural", "en-US-DavisNeural",
66
- "en-US-ElizabethNeural", "en-US-EricNeural",
67
- "en-US-GinaNeural", "en-US-JacobNeural",
68
- "en-US-JaneNeural", "en-US-JasonNeural",
69
- "en-US-MichelleNeural", "en-US-MonicaNeural",
70
- "en-US-SaraNeural", "en-US-SteffanNeural",
71
- "en-US-TonyNeural", "en-US-YaraNeural",
72
- "fr-FR-AlainNeural", "fr-FR-BrigitteNeural",
73
- "fr-FR-CelesteNeural", "fr-FR-ClaudeNeural",
74
- "fr-FR-CoralieNeural", "fr-FR-DeniseNeural",
75
- "fr-FR-EloiseNeural", "fr-FR-HenriNeural",
76
- "fr-FR-JacquelineNeural", "fr-FR-JeromeNeural",
77
- "fr-FR-JosephineNeural", "fr-FR-MauriceNeural",
78
- "fr-FR-YvesNeural", "fr-FR-YvetteNeural",
79
- "de-DE-AmalaNeural", "de-DE-BerndNeural",
80
- "de-DE-ChristophNeural", "de-DE-ConradNeural",
81
- "de-DE-ElkeNeural", "de-DE-GiselaNeural",
82
- "de-DE-KasperNeural", "de-DE-KatjaNeural",
83
- "de-DE-KillianNeural", "de-DE-KlarissaNeural",
84
- "de-DE-KlausNeural", "de-DE-LouisaNeural",
85
- "de-DE-MajaNeural", "de-DE-RalfNeural",
86
- "de-DE-TanjaNeural", "de-DE-ViktoriaNeural",
87
- "it-IT-BenignoNeural", "it-IT-CalimeroNeural",
88
- "it-IT-CataldoNeural", "it-IT-DiegoNeural",
89
- "it-IT-ElsaNeural", "it-IT-FabiolaNeural",
90
- "it-IT-GianniNeural", "it-IT-ImeldaNeural",
91
- "it-IT-IrmaNeural", "it-IT-IsabellaNeural",
92
- "it-IT-LisandroNeural", "it-IT-PalmiraNeural",
93
- "it-IT-PierinaNeural", "it-IT-RinaldoNeural",
94
- "ja-JP-AoiNeural", "ja-JP-DaichiNeural",
95
- "ja-JP-HarukaNeural", "ja-JP-KeitaNeural",
96
- "ja-JP-MayuNeural", "ja-JP-NanamiNeural",
97
- "ja-JP-NaokiNeural", "ja-JP-ShioriNeural"
98
  ]
99
  if voz not in voces_validas:
100
  raise ValueError(f"La voz seleccionada '{voz}' no es válida.")
@@ -137,6 +119,11 @@ async def procesar_video(video_input, texto_tts, voz_seleccionada):
137
  intro, outro, video_original = None, None, None
138
  try:
139
  logging.info("Iniciando procesamiento")
 
 
 
 
 
140
  video_original = VideoFileClip(video_input, target_resolution=(720, 1280))
141
  duracion_video = video_original.duration
142
 
@@ -207,7 +194,7 @@ async def procesar_video(video_input, texto_tts, voz_seleccionada):
207
  video_original.close()
208
  if intro:
209
  intro.close()
210
- if outro:
211
  outro.close()
212
  for file in temp_files:
213
  try:
 
37
  from threading import Timer
38
  Timer(delay, eliminar).start()
39
 
40
+ def validar_video(video_path):
41
+ try:
42
+ clip = VideoFileClip(video_path)
43
+ clip.close()
44
+ return True
45
+ except Exception as e:
46
+ logging.error(f"El video no es válido: {e}")
47
+ return False
48
+
49
+ def convertir_video(video_path):
50
+ try:
51
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_converted:
52
+ output_path = tmp_converted.name
53
+ os.system(f'ffmpeg -i "{video_path}" -vcodec libx264 -acodec aac "{output_path}" -y')
54
+ return output_path
55
+ except Exception as e:
56
+ logging.error(f"Error al convertir el video: {e}")
57
+ raise
58
+
59
+ def ajustar_resolucion(video_path):
60
+ try:
61
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_resized:
62
+ output_path = tmp_resized.name
63
+ os.system(f'ffmpeg -i "{video_path}" -vf "scale=1280:720" -vcodec libx264 -acodec aac "{output_path}" -y')
64
+ return output_path
65
+ except Exception as e:
66
+ logging.error(f"Error al ajustar la resolución del video: {e}")
67
+ raise
68
+
69
  async def generar_tts(texto, voz, duracion_total):
70
  try:
 
71
  if not texto.strip():
72
  raise ValueError("El texto para TTS no puede estar vacío.")
73
+ if len(texto) > 1000:
74
+ texto = texto[:1000]
75
 
 
76
  voces_validas = [
77
  "es-ES-AlvaroNeural", "es-MX-BeatrizNeural",
78
  "es-ES-ElviraNeural", "es-MX-JavierNeural",
79
+ # Añade todas las voces válidas aquí
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  ]
81
  if voz not in voces_validas:
82
  raise ValueError(f"La voz seleccionada '{voz}' no es válida.")
 
119
  intro, outro, video_original = None, None, None
120
  try:
121
  logging.info("Iniciando procesamiento")
122
+
123
+ if not validar_video(video_input):
124
+ video_input = convertir_video(video_input)
125
+ temp_files.append(video_input)
126
+
127
  video_original = VideoFileClip(video_input, target_resolution=(720, 1280))
128
  duracion_video = video_original.duration
129
 
 
194
  video_original.close()
195
  if intro:
196
  intro.close()
197
+ if otro:
198
  outro.close()
199
  for file in temp_files:
200
  try: