Update app.py
Browse files
app.py
CHANGED
@@ -9,17 +9,21 @@ import asyncio
|
|
9 |
import os
|
10 |
import time
|
11 |
import requests
|
12 |
-
from googleapiclient.discovery import build
|
13 |
-
from googleapiclient.http import MediaFileUpload
|
14 |
import tempfile
|
15 |
import re
|
16 |
import random
|
17 |
from google_drive_upload import upload_to_google_drive # Importa la nueva funci贸n
|
18 |
|
19 |
-
# Define la carpeta de salida
|
20 |
output_folder = "outputs"
|
21 |
os.makedirs(output_folder, exist_ok=True)
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
def clean_text_for_search(text):
|
24 |
"""Limpia el texto para hacer b煤squedas v谩lidas en Pixabay"""
|
25 |
text = re.sub(r'[^\w\s]', '', text).strip()
|
@@ -140,7 +144,7 @@ def combine_audio_video(audio_file, video_clip, music_clip=None):
|
|
140 |
|
141 |
except Exception as e:
|
142 |
print(f"Error combinando audio y video: {e}")
|
143 |
-
return
|
144 |
|
145 |
def process_input(text, txt_file, mp3_file, selected_voice, rate, pitch, keywords):
|
146 |
"""Procesa la entrada del usuario y genera el video final."""
|
@@ -150,21 +154,23 @@ def process_input(text, txt_file, mp3_file, selected_voice, rate, pitch, keyword
|
|
150 |
elif txt_file is not None:
|
151 |
final_text = txt_file.decode("utf-8")
|
152 |
else:
|
153 |
-
return "
|
154 |
|
155 |
voices = asyncio.run(get_voices())
|
156 |
if selected_voice not in voices:
|
157 |
-
return
|
158 |
|
159 |
try:
|
160 |
audio_file = asyncio.run(text_to_speech(final_text, selected_voice, rate, pitch))
|
161 |
except Exception as e:
|
162 |
-
|
|
|
163 |
|
164 |
try:
|
165 |
video_clip = concatenate_pixabay_videos(keywords, num_videos_per_keyword=1)
|
166 |
except Exception as e:
|
167 |
-
|
|
|
168 |
|
169 |
if mp3_file is not None:
|
170 |
music_clip = adjust_background_music(video_clip.duration, mp3_file.name)
|
@@ -172,11 +178,12 @@ def process_input(text, txt_file, mp3_file, selected_voice, rate, pitch, keyword
|
|
172 |
music_clip = None
|
173 |
|
174 |
final_video_path = combine_audio_video(audio_file, video_clip, music_clip)
|
175 |
-
upload_to_google_drive(final_video_path) # Sube a Google Drive
|
176 |
return final_video_path
|
177 |
|
178 |
except Exception as e:
|
179 |
-
|
|
|
180 |
|
181 |
# Interfaz Gradio
|
182 |
with gr.Blocks() as demo:
|
|
|
9 |
import os
|
10 |
import time
|
11 |
import requests
|
|
|
|
|
12 |
import tempfile
|
13 |
import re
|
14 |
import random
|
15 |
from google_drive_upload import upload_to_google_drive # Importa la nueva funci贸n
|
16 |
|
17 |
+
# Define la carpeta de salida
|
18 |
output_folder = "outputs"
|
19 |
os.makedirs(output_folder, exist_ok=True)
|
20 |
|
21 |
+
# Crear un video predeterminado si no existe
|
22 |
+
if not os.path.exists(os.path.join(output_folder, "blank.mp4")):
|
23 |
+
from moviepy.editor import VideoClip
|
24 |
+
blank_clip = VideoClip(lambda t: None, duration=1) # Video vac铆o de 1 segundo
|
25 |
+
blank_clip.write_videofile(os.path.join(output_folder, "blank.mp4"), fps=24)
|
26 |
+
|
27 |
def clean_text_for_search(text):
|
28 |
"""Limpia el texto para hacer b煤squedas v谩lidas en Pixabay"""
|
29 |
text = re.sub(r'[^\w\s]', '', text).strip()
|
|
|
144 |
|
145 |
except Exception as e:
|
146 |
print(f"Error combinando audio y video: {e}")
|
147 |
+
return os.path.join(output_folder, "blank.mp4") # Devuelve un video predeterminado en caso de error
|
148 |
|
149 |
def process_input(text, txt_file, mp3_file, selected_voice, rate, pitch, keywords):
|
150 |
"""Procesa la entrada del usuario y genera el video final."""
|
|
|
154 |
elif txt_file is not None:
|
155 |
final_text = txt_file.decode("utf-8")
|
156 |
else:
|
157 |
+
return os.path.join(output_folder, "blank.mp4") # Devuelve un video predeterminado si no hay entrada
|
158 |
|
159 |
voices = asyncio.run(get_voices())
|
160 |
if selected_voice not in voices:
|
161 |
+
return os.path.join(output_folder, "blank.mp4") # Devuelve un video predeterminado si la voz no es v谩lida
|
162 |
|
163 |
try:
|
164 |
audio_file = asyncio.run(text_to_speech(final_text, selected_voice, rate, pitch))
|
165 |
except Exception as e:
|
166 |
+
print(f"Error generando audio: {e}")
|
167 |
+
return os.path.join(output_folder, "blank.mp4") # Devuelve un video predeterminado en caso de error
|
168 |
|
169 |
try:
|
170 |
video_clip = concatenate_pixabay_videos(keywords, num_videos_per_keyword=1)
|
171 |
except Exception as e:
|
172 |
+
print(f"Error concatenando videos: {e}")
|
173 |
+
return os.path.join(output_folder, "blank.mp4") # Devuelve un video predeterminado en caso de error
|
174 |
|
175 |
if mp3_file is not None:
|
176 |
music_clip = adjust_background_music(video_clip.duration, mp3_file.name)
|
|
|
178 |
music_clip = None
|
179 |
|
180 |
final_video_path = combine_audio_video(audio_file, video_clip, music_clip)
|
181 |
+
upload_to_google_drive(final_video_path) # Sube a Google Drive
|
182 |
return final_video_path
|
183 |
|
184 |
except Exception as e:
|
185 |
+
print(f"Error durante el procesamiento: {e}")
|
186 |
+
return os.path.join(output_folder, "blank.mp4") # Devuelve un video predeterminado en caso de error
|
187 |
|
188 |
# Interfaz Gradio
|
189 |
with gr.Blocks() as demo:
|