Update app.py
Browse files
app.py
CHANGED
@@ -84,63 +84,101 @@ def resize_and_blur_video(clip, target_width=1920, target_height=1080):
|
|
84 |
print(f"Error en resize_and_blur_video: {e}")
|
85 |
return clip
|
86 |
|
87 |
-
def concatenate_pexels_videos(keywords, num_videos_per_keyword=1):
|
88 |
"""
|
89 |
-
Concatena videos de Pexels
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
91 |
"""
|
92 |
keyword_list = [keyword.strip() for keyword in keywords.split(",") if keyword.strip()]
|
93 |
if not keyword_list:
|
94 |
-
raise
|
95 |
|
96 |
video_clips = []
|
|
|
97 |
|
98 |
for keyword in keyword_list:
|
99 |
try:
|
100 |
-
print(f"Buscando videos para
|
101 |
-
|
|
|
|
|
|
|
|
|
102 |
if not links:
|
103 |
-
print(f"No se encontraron videos para
|
104 |
-
continue
|
105 |
-
|
106 |
-
link = links[0] # Usamos solo el primer video encontrado
|
107 |
-
video_response = requests.get(link)
|
108 |
-
if video_response.status_code != 200:
|
109 |
-
print(f"Error al descargar video desde {link}: C贸digo de estado {video_response.status_code}")
|
110 |
continue
|
111 |
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
except Exception as e:
|
119 |
print(f"Error procesando palabra clave '{keyword}': {e}")
|
120 |
continue
|
121 |
|
122 |
if not video_clips:
|
123 |
-
raise Exception("No se pudieron obtener videos v谩lidos.")
|
124 |
|
125 |
-
|
|
|
|
|
126 |
random.shuffle(video_clips)
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
def adjust_background_music(video_duration, music_file):
|
131 |
try:
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
music_clips = [music] * repetitions
|
136 |
-
music = concatenate_audioclips(music_clips)
|
137 |
-
if music.duration > video_duration:
|
138 |
-
music = music.subclip(0, video_duration)
|
139 |
-
music = music.volumex(0.2)
|
140 |
-
return music
|
141 |
except Exception as e:
|
142 |
-
|
143 |
-
return None
|
144 |
|
145 |
def combine_audio_video(audio_file, video_clip, music_clip=None):
|
146 |
try:
|
|
|
84 |
print(f"Error en resize_and_blur_video: {e}")
|
85 |
return clip
|
86 |
|
87 |
+
def concatenate_pexels_videos(keywords, num_videos_per_keyword=1, target_width=1920, target_height=1080):
|
88 |
"""
|
89 |
+
Concatena videos de Pexels manteniendo una calidad y resoluci贸n consistentes.
|
90 |
+
|
91 |
+
Args:
|
92 |
+
keywords (str): Palabras clave separadas por comas
|
93 |
+
num_videos_per_keyword (int): N煤mero de videos por palabra clave
|
94 |
+
target_width (int): Ancho objetivo para los videos
|
95 |
+
target_height (int): Alto objetivo para los videos
|
96 |
"""
|
97 |
keyword_list = [keyword.strip() for keyword in keywords.split(",") if keyword.strip()]
|
98 |
if not keyword_list:
|
99 |
+
raise ValueError("No se proporcionaron palabras clave v谩lidas.")
|
100 |
|
101 |
video_clips = []
|
102 |
+
processed_keywords = []
|
103 |
|
104 |
for keyword in keyword_list:
|
105 |
try:
|
106 |
+
print(f"Buscando videos para: '{keyword}'...")
|
107 |
+
# Limpiar la palabra clave para b煤squeda
|
108 |
+
clean_keyword = clean_text_for_search(keyword)
|
109 |
+
|
110 |
+
# Obtener videos con la nueva API
|
111 |
+
links = search_pexels(clean_keyword, num_results=num_videos_per_keyword)
|
112 |
if not links:
|
113 |
+
print(f"No se encontraron videos para: '{keyword}'")
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
continue
|
115 |
|
116 |
+
for link in links:
|
117 |
+
try:
|
118 |
+
print(f"Descargando video para: '{keyword}'...")
|
119 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_video:
|
120 |
+
video_response = requests.get(link, stream=True)
|
121 |
+
if video_response.status_code != 200:
|
122 |
+
print(f"Error descargando video: {video_response.status_code}")
|
123 |
+
continue
|
124 |
+
|
125 |
+
# Guardar el video
|
126 |
+
for chunk in video_response.iter_content(chunk_size=8192):
|
127 |
+
if chunk:
|
128 |
+
tmp_video.write(chunk)
|
129 |
+
|
130 |
+
tmp_video.flush()
|
131 |
+
|
132 |
+
# Procesar el video
|
133 |
+
try:
|
134 |
+
clip = VideoFileClip(tmp_video.name)
|
135 |
+
|
136 |
+
# Verificar duraci贸n m铆nima
|
137 |
+
if clip.duration < 3:
|
138 |
+
print(f"Video demasiado corto ({clip.duration}s), saltando...")
|
139 |
+
clip.close()
|
140 |
+
continue
|
141 |
+
|
142 |
+
# Procesar y agregar el clip
|
143 |
+
processed_clip = resize_and_blur_video(clip, target_width, target_height)
|
144 |
+
if processed_clip:
|
145 |
+
video_clips.append(processed_clip)
|
146 |
+
processed_keywords.append(keyword)
|
147 |
+
print(f"Video procesado exitosamente para: '{keyword}'")
|
148 |
+
|
149 |
+
except Exception as e:
|
150 |
+
print(f"Error procesando video: {e}")
|
151 |
+
if 'clip' in locals():
|
152 |
+
clip.close()
|
153 |
+
continue
|
154 |
+
|
155 |
+
finally:
|
156 |
+
# Limpiar archivo temporal
|
157 |
+
if os.path.exists(tmp_video.name):
|
158 |
+
try:
|
159 |
+
os.unlink(tmp_video.name)
|
160 |
+
except Exception as e:
|
161 |
+
print(f"Error eliminando archivo temporal: {e}")
|
162 |
|
163 |
except Exception as e:
|
164 |
print(f"Error procesando palabra clave '{keyword}': {e}")
|
165 |
continue
|
166 |
|
167 |
if not video_clips:
|
168 |
+
raise Exception("No se pudieron obtener videos v谩lidos para ninguna palabra clave.")
|
169 |
|
170 |
+
print(f"Videos procesados exitosamente para las palabras: {', '.join(processed_keywords)}")
|
171 |
+
|
172 |
+
# Aleatorizar el orden de los clips
|
173 |
random.shuffle(video_clips)
|
174 |
+
|
175 |
+
# Concatenar los clips
|
|
|
|
|
176 |
try:
|
177 |
+
final_clip = concatenate_videoclips(video_clips, method="compose")
|
178 |
+
print(f"Video final generado: {final_clip.size}")
|
179 |
+
return final_clip
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
except Exception as e:
|
181 |
+
raise Exception(f"Error concatenando clips: {e}")
|
|
|
182 |
|
183 |
def combine_audio_video(audio_file, video_clip, music_clip=None):
|
184 |
try:
|