Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -11,8 +11,6 @@ import os
|
|
11 |
import json
|
12 |
import time
|
13 |
import requests
|
14 |
-
import tempfile
|
15 |
-
import re
|
16 |
import random
|
17 |
from google_drive_upload import authenticate_google_drive, upload_to_google_drive
|
18 |
from io import BytesIO
|
@@ -23,13 +21,25 @@ if service_account_info:
|
|
23 |
with open('service-account.json', 'w') as f:
|
24 |
json.dump(service_account_info, f)
|
25 |
|
26 |
-
# Define la carpeta de salida
|
27 |
output_folder = "outputs"
|
|
|
28 |
os.makedirs(output_folder, exist_ok=True)
|
|
|
29 |
|
30 |
# ID de la carpeta de destino en Google Drive
|
31 |
FOLDER_ID = "12S6adpanAXjf71pKKGRRPqpzbJa5XEh3" # Reemplaza con tu ID de carpeta
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
def resize_and_blur_video(clip, target_width=1920, target_height=1080):
|
34 |
"""Redimensiona el video al tamaño 1080p (16:9) y aplica desenfoque si es necesario."""
|
35 |
try:
|
@@ -66,6 +76,20 @@ def resize_and_blur_video(clip, target_width=1920, target_height=1080):
|
|
66 |
print(f"Error en resize_and_blur_video: {e}")
|
67 |
return clip
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
def concatenate_pixabay_videos(keywords, num_videos_per_keyword=1):
|
70 |
"""Concatena videos de Pixabay basados en palabras clave."""
|
71 |
keyword_list = [keyword.strip() for keyword in keywords.split(",") if keyword.strip()]
|
@@ -81,17 +105,12 @@ def concatenate_pixabay_videos(keywords, num_videos_per_keyword=1):
|
|
81 |
links = search_pixabay("nature", num_results=num_videos_per_keyword)
|
82 |
if not links:
|
83 |
continue
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
print(f"Error al descargar video desde {link}")
|
88 |
-
continue
|
89 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_video:
|
90 |
-
tmp_video.write(video_response.content)
|
91 |
-
clip = VideoFileClip(tmp_video.name)
|
92 |
processed_clip = resize_and_blur_video(clip)
|
93 |
video_clips.append(processed_clip)
|
94 |
-
os.
|
95 |
except Exception as e:
|
96 |
print(f"Error procesando palabra clave '{keyword}': {e}")
|
97 |
continue
|
@@ -189,6 +208,8 @@ def process_input(text, txt_file, mp3_file, selected_voice, rate, pitch, keyword
|
|
189 |
except Exception as e:
|
190 |
print(f"Error durante el procesamiento: {e}")
|
191 |
return None
|
|
|
|
|
192 |
|
193 |
# Interfaz Gradio
|
194 |
with gr.Blocks() as demo:
|
|
|
11 |
import json
|
12 |
import time
|
13 |
import requests
|
|
|
|
|
14 |
import random
|
15 |
from google_drive_upload import authenticate_google_drive, upload_to_google_drive
|
16 |
from io import BytesIO
|
|
|
21 |
with open('service-account.json', 'w') as f:
|
22 |
json.dump(service_account_info, f)
|
23 |
|
24 |
+
# Define la carpeta de salida y la carpeta temporal
|
25 |
output_folder = "outputs"
|
26 |
+
temp_dir = "temp_files"
|
27 |
os.makedirs(output_folder, exist_ok=True)
|
28 |
+
os.makedirs(temp_dir, exist_ok=True)
|
29 |
|
30 |
# ID de la carpeta de destino en Google Drive
|
31 |
FOLDER_ID = "12S6adpanAXjf71pKKGRRPqpzbJa5XEh3" # Reemplaza con tu ID de carpeta
|
32 |
|
33 |
+
def cleanup_temp_files():
|
34 |
+
"""Elimina todos los archivos temporales de la carpeta temp_files."""
|
35 |
+
for filename in os.listdir(temp_dir):
|
36 |
+
file_path = os.path.join(temp_dir, filename)
|
37 |
+
try:
|
38 |
+
if os.path.isfile(file_path):
|
39 |
+
os.remove(file_path)
|
40 |
+
except Exception as e:
|
41 |
+
print(f"Error deleting {file_path}: {e}")
|
42 |
+
|
43 |
def resize_and_blur_video(clip, target_width=1920, target_height=1080):
|
44 |
"""Redimensiona el video al tamaño 1080p (16:9) y aplica desenfoque si es necesario."""
|
45 |
try:
|
|
|
76 |
print(f"Error en resize_and_blur_video: {e}")
|
77 |
return clip
|
78 |
|
79 |
+
def download_video(link):
|
80 |
+
"""Descarga un video desde un enlace y lo guarda en la carpeta temporal."""
|
81 |
+
try:
|
82 |
+
video_response = requests.get(link)
|
83 |
+
if video_response.status_code != 200:
|
84 |
+
return None
|
85 |
+
temp_video_path = os.path.join(temp_dir, f"temp_video_{int(time.time())}.mp4")
|
86 |
+
with open(temp_video_path, "wb") as f:
|
87 |
+
f.write(video_response.content)
|
88 |
+
return temp_video_path
|
89 |
+
except Exception as e:
|
90 |
+
print(f"Error downloading video: {e}")
|
91 |
+
return None
|
92 |
+
|
93 |
def concatenate_pixabay_videos(keywords, num_videos_per_keyword=1):
|
94 |
"""Concatena videos de Pixabay basados en palabras clave."""
|
95 |
keyword_list = [keyword.strip() for keyword in keywords.split(",") if keyword.strip()]
|
|
|
105 |
links = search_pixabay("nature", num_results=num_videos_per_keyword)
|
106 |
if not links:
|
107 |
continue
|
108 |
+
temp_video_path = download_video(links[0])
|
109 |
+
if temp_video_path:
|
110 |
+
clip = VideoFileClip(temp_video_path)
|
|
|
|
|
|
|
|
|
|
|
111 |
processed_clip = resize_and_blur_video(clip)
|
112 |
video_clips.append(processed_clip)
|
113 |
+
os.remove(temp_video_path) # Eliminar archivo temporal después de usarlo
|
114 |
except Exception as e:
|
115 |
print(f"Error procesando palabra clave '{keyword}': {e}")
|
116 |
continue
|
|
|
208 |
except Exception as e:
|
209 |
print(f"Error durante el procesamiento: {e}")
|
210 |
return None
|
211 |
+
finally:
|
212 |
+
cleanup_temp_files() # Limpiar archivos temporales al finalizar
|
213 |
|
214 |
# Interfaz Gradio
|
215 |
with gr.Blocks() as demo:
|