gnosticdev commited on
Commit
6065fb3
verified
1 Parent(s): 532eeac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -19
app.py CHANGED
@@ -3,10 +3,9 @@ from tts_module import get_voices, text_to_speech
3
  from pixabay_api import search_pixabay
4
  from moviepy.editor import (
5
  AudioFileClip, VideoFileClip, CompositeAudioClip,
6
- concatenate_audioclips, concatenate_videoclips, vfx, CompositeVideoClip,
7
  ColorClip
8
  )
9
- from scipy.ndimage import gaussian_filter
10
  import asyncio
11
  import os
12
  import json
@@ -23,12 +22,14 @@ if service_account_info:
23
  with open('service-account.json', 'w') as f:
24
  json.dump(service_account_info, f)
25
 
 
26
  output_folder = "outputs"
27
  temp_dir = "temp_files"
28
  os.makedirs(output_folder, exist_ok=True)
29
  os.makedirs(temp_dir, exist_ok=True)
30
 
31
- FOLDER_ID = "12S6adpanAXjf71pKKGRRPqpzbJa5XEh3"
 
32
 
33
  def cleanup_temp_files():
34
  """Elimina todos los archivos temporales de la carpeta temp_files."""
@@ -40,25 +41,16 @@ def cleanup_temp_files():
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:
46
  w, h = clip.size
47
  current_aspect_ratio = w / h
48
  target_aspect_ratio = target_width / target_height
49
 
50
  if abs(current_aspect_ratio - target_aspect_ratio) < 0.1:
51
- # Si la relaci贸n de aspecto ya es cercana a 16:9, solo redimensionamos
52
  return clip.resize((target_width, target_height))
53
 
54
- # Crear un fondo borroso con las dimensiones objetivo
55
- background = ColorClip(size=(target_width, target_height), color=[0, 0, 0]).set_duration(clip.duration)
56
- try:
57
- # Aplicar filtro gaussiano como alternativa al blur
58
- background = background.fx(vfx, lambda gf: gaussian_filter(gf, sigma=5))
59
- except Exception as e:
60
- print(f"Error al aplicar filtro gaussiano: {e}")
61
-
62
  # Redimensionar el video original para mantener su proporci贸n
63
  if current_aspect_ratio < target_aspect_ratio: # Video vertical
64
  new_height = target_height
@@ -71,10 +63,11 @@ def resize_and_blur_video(clip, target_width=1920, target_height=1080):
71
  y_center = (target_height - new_height) / 2
72
  resized_clip = clip.resize(height=new_height).set_position((0, y_center))
73
 
74
- # Combinar el fondo borroso con el video redimensionado
 
75
  return CompositeVideoClip([background, resized_clip], size=(target_width, target_height))
76
  except Exception as e:
77
- print(f"Error en resize_and_blur_video: {e}")
78
  return clip
79
 
80
  def download_video(link):
@@ -109,7 +102,7 @@ def concatenate_pixabay_videos(keywords, num_videos_per_keyword=1):
109
  temp_video_path = download_video(links[0])
110
  if temp_video_path:
111
  clip = VideoFileClip(temp_video_path)
112
- processed_clip = resize_and_blur_video(clip)
113
  video_clips.append(processed_clip)
114
  os.remove(temp_video_path) # Eliminar archivo temporal despu茅s de usarlo
115
  except Exception as e:
@@ -202,24 +195,30 @@ def upload_to_google_drive(file_path, folder_id):
202
  def process_input(text, txt_file, mp3_file, selected_voice, rate, pitch, keywords):
203
  """Procesa la entrada del usuario y genera el video final."""
204
  try:
 
205
  if text.strip():
206
  final_text = text
207
  elif txt_file is not None:
208
  final_text = txt_file.decode("utf-8")
209
  else:
210
  raise ValueError("No text input provided")
 
211
  audio_file = asyncio.run(text_to_speech(final_text, selected_voice, rate, pitch))
212
  if not audio_file:
213
  raise ValueError("Failed to generate audio")
 
214
  video_clip = concatenate_pixabay_videos(keywords, num_videos_per_keyword=1)
215
  if not video_clip:
216
  raise ValueError("Failed to generate video")
 
217
  music_clip = None
218
  if mp3_file is not None:
219
  music_clip = adjust_background_music(video_clip.duration, mp3_file.name)
 
220
  final_video_path = combine_audio_video(audio_file, video_clip, music_clip)
221
  if not final_video_path:
222
  raise ValueError("Failed to combine audio and video")
 
223
  download_link = upload_to_google_drive(final_video_path, folder_id=FOLDER_ID)
224
  if download_link:
225
  return f"[Descargar video]({download_link})"
@@ -229,8 +228,9 @@ def process_input(text, txt_file, mp3_file, selected_voice, rate, pitch, keyword
229
  print(f"Error durante el procesamiento: {e}")
230
  return None
231
  finally:
232
- cleanup_temp_files()
233
 
 
234
  with gr.Blocks() as demo:
235
  gr.Markdown("# Text-to-Video Generator")
236
  with gr.Row():
@@ -247,7 +247,8 @@ with gr.Blocks() as demo:
247
  rate_slider = gr.Slider(minimum=-50, maximum=50, value=0, label="Speech Rate Adjustment (%)", step=1)
248
  pitch_slider = gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1)
249
  with gr.Column():
250
- output_link = gr.Markdown("")
 
251
  btn = gr.Button("Generate Video")
252
  btn.click(
253
  process_input,
@@ -255,5 +256,8 @@ with gr.Blocks() as demo:
255
  outputs=output_link
256
  )
257
 
 
258
  port = int(os.getenv("PORT", 7860))
 
 
259
  demo.launch(server_name="0.0.0.0", server_port=port, share=True, show_error=True)
 
3
  from pixabay_api import search_pixabay
4
  from moviepy.editor import (
5
  AudioFileClip, VideoFileClip, CompositeAudioClip,
6
+ concatenate_audioclips, concatenate_videoclips, CompositeVideoClip,
7
  ColorClip
8
  )
 
9
  import asyncio
10
  import os
11
  import json
 
22
  with open('service-account.json', 'w') as f:
23
  json.dump(service_account_info, f)
24
 
25
+ # Define la carpeta de salida y la carpeta temporal
26
  output_folder = "outputs"
27
  temp_dir = "temp_files"
28
  os.makedirs(output_folder, exist_ok=True)
29
  os.makedirs(temp_dir, exist_ok=True)
30
 
31
+ # ID de la carpeta de destino en Google Drive
32
+ FOLDER_ID = "12S6adpanAXjf71pKKGRRPqpzbJa5XEh3" # Reemplaza con tu ID de carpeta
33
 
34
  def cleanup_temp_files():
35
  """Elimina todos los archivos temporales de la carpeta temp_files."""
 
41
  except Exception as e:
42
  print(f"Error deleting {file_path}: {e}")
43
 
44
+ def resize_video(clip, target_width=1920, target_height=1080):
45
+ """Redimensiona el video al tama帽o 1080p (16:9) sin aplicar desenfoque."""
46
  try:
47
  w, h = clip.size
48
  current_aspect_ratio = w / h
49
  target_aspect_ratio = target_width / target_height
50
 
51
  if abs(current_aspect_ratio - target_aspect_ratio) < 0.1:
 
52
  return clip.resize((target_width, target_height))
53
 
 
 
 
 
 
 
 
 
54
  # Redimensionar el video original para mantener su proporci贸n
55
  if current_aspect_ratio < target_aspect_ratio: # Video vertical
56
  new_height = target_height
 
63
  y_center = (target_height - new_height) / 2
64
  resized_clip = clip.resize(height=new_height).set_position((0, y_center))
65
 
66
+ # Crear un fondo negro y combinar con el video redimensionado
67
+ background = ColorClip(size=(target_width, target_height), color=[0, 0, 0]).set_duration(clip.duration)
68
  return CompositeVideoClip([background, resized_clip], size=(target_width, target_height))
69
  except Exception as e:
70
+ print(f"Error en resize_video: {e}")
71
  return clip
72
 
73
  def download_video(link):
 
102
  temp_video_path = download_video(links[0])
103
  if temp_video_path:
104
  clip = VideoFileClip(temp_video_path)
105
+ processed_clip = resize_video(clip)
106
  video_clips.append(processed_clip)
107
  os.remove(temp_video_path) # Eliminar archivo temporal despu茅s de usarlo
108
  except Exception as e:
 
195
  def process_input(text, txt_file, mp3_file, selected_voice, rate, pitch, keywords):
196
  """Procesa la entrada del usuario y genera el video final."""
197
  try:
198
+ # Determinamos el texto a usar
199
  if text.strip():
200
  final_text = text
201
  elif txt_file is not None:
202
  final_text = txt_file.decode("utf-8")
203
  else:
204
  raise ValueError("No text input provided")
205
+ # Generamos el audio
206
  audio_file = asyncio.run(text_to_speech(final_text, selected_voice, rate, pitch))
207
  if not audio_file:
208
  raise ValueError("Failed to generate audio")
209
+ # Generamos el video
210
  video_clip = concatenate_pixabay_videos(keywords, num_videos_per_keyword=1)
211
  if not video_clip:
212
  raise ValueError("Failed to generate video")
213
+ # Procesamos la m煤sica de fondo si existe
214
  music_clip = None
215
  if mp3_file is not None:
216
  music_clip = adjust_background_music(video_clip.duration, mp3_file.name)
217
+ # Combinamos todo
218
  final_video_path = combine_audio_video(audio_file, video_clip, music_clip)
219
  if not final_video_path:
220
  raise ValueError("Failed to combine audio and video")
221
+ # Subimos a Google Drive y obtenemos el enlace
222
  download_link = upload_to_google_drive(final_video_path, folder_id=FOLDER_ID)
223
  if download_link:
224
  return f"[Descargar video]({download_link})"
 
228
  print(f"Error durante el procesamiento: {e}")
229
  return None
230
  finally:
231
+ cleanup_temp_files() # Limpiar archivos temporales al finalizar
232
 
233
+ # Interfaz Gradio
234
  with gr.Blocks() as demo:
235
  gr.Markdown("# Text-to-Video Generator")
236
  with gr.Row():
 
247
  rate_slider = gr.Slider(minimum=-50, maximum=50, value=0, label="Speech Rate Adjustment (%)", step=1)
248
  pitch_slider = gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1)
249
  with gr.Column():
250
+ output_link = gr.Markdown("") # Mostrar el enlace de descarga
251
+
252
  btn = gr.Button("Generate Video")
253
  btn.click(
254
  process_input,
 
256
  outputs=output_link
257
  )
258
 
259
+ # Leer el puerto asignado por Hugging Face
260
  port = int(os.getenv("PORT", 7860))
261
+
262
+ # Lanzar la aplicaci贸n
263
  demo.launch(server_name="0.0.0.0", server_port=port, share=True, show_error=True)