gnosticdev commited on
Commit
9d50291
verified
1 Parent(s): bf377f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -11
app.py CHANGED
@@ -3,8 +3,8 @@ import moviepy.editor as mp
3
  import numpy as np
4
  import librosa
5
  from PIL import Image, ImageDraw
6
- import os
7
  import tempfile
 
8
  import logging
9
 
10
  # Configuraci贸n de logging
@@ -17,8 +17,11 @@ logger = logging.getLogger("audio_to_video")
17
 
18
  def generate_video(audio_file, image_file):
19
  try:
 
 
20
  # 1. Cargar audio
21
- y, sr = librosa.load(audio_file)
 
22
  duration = librosa.get_duration(y=y, sr=sr)
23
  logger.info(f"Audio cargado: {duration:.1f} segundos")
24
 
@@ -32,10 +35,12 @@ def generate_video(audio_file, image_file):
32
  audio_envelope_zoom = audio_envelope * 0.2 + 0.9 # Para zoom
33
  audio_envelope_wave = audio_envelope * (img_h // 6) # Para waveform
34
 
35
- # 4. Generar frames
36
  def make_frame(t):
37
- # --- Zoom autom谩tico ---
38
  time_idx = int(t * sr)
 
 
39
  zoom_factor = audio_envelope_zoom[time_idx] if time_idx < len(audio_envelope_zoom) else 1.0
40
  new_size = (int(img_w * zoom_factor), int(img_h * zoom_factor))
41
  zoomed_img = img.resize(new_size, Image.LANCZOS)
@@ -50,7 +55,7 @@ def generate_video(audio_file, image_file):
50
  y_offset + img_h
51
  ))
52
 
53
- # --- Waveform ---
54
  frame = ImageDraw.Draw(cropped_img)
55
  start_y = int(img_h * 0.8) # 80% hacia abajo
56
 
@@ -68,7 +73,7 @@ def generate_video(audio_file, image_file):
68
  points.extend([(x, y_pos), (x, y_neg)])
69
 
70
  if len(points) > 2:
71
- frame.polygon(points, fill=(255, 0, 0, 150))
72
 
73
  return np.array(cropped_img)
74
 
@@ -77,10 +82,11 @@ def generate_video(audio_file, image_file):
77
  video.fps = 24
78
  video = video.set_audio(mp.AudioFileClip(audio_file))
79
 
80
- # 6. Guardar video
81
  temp_dir = tempfile.mkdtemp()
82
  output_path = os.path.join(temp_dir, "output.mp4")
83
 
 
84
  video.write_videofile(
85
  output_path,
86
  codec="libx264",
@@ -89,12 +95,16 @@ def generate_video(audio_file, image_file):
89
  logger=None
90
  )
91
 
92
- logger.info(f"Video guardado en: {output_path}")
 
 
 
 
93
  return output_path # Retornar la ruta completa
94
 
95
  except Exception as e:
96
- logger.error(f"Error cr铆tico: {str(e)}")
97
- return None # Retornar None en caso de error
98
 
99
  # Interfaz Gradio
100
  iface = gr.Interface(
@@ -109,4 +119,4 @@ iface = gr.Interface(
109
  )
110
 
111
  if __name__ == "__main__":
112
- iface.queue().launch()
 
3
  import numpy as np
4
  import librosa
5
  from PIL import Image, ImageDraw
 
6
  import tempfile
7
+ import os
8
  import logging
9
 
10
  # Configuraci贸n de logging
 
17
 
18
  def generate_video(audio_file, image_file):
19
  try:
20
+ logger.info("Iniciando generaci贸n del video...")
21
+
22
  # 1. Cargar audio
23
+ logger.info(f"Cargando audio: {audio_file}")
24
+ y, sr = librosa.load(audio_file, sr=None, mono=True) # Carga completa del audio
25
  duration = librosa.get_duration(y=y, sr=sr)
26
  logger.info(f"Audio cargado: {duration:.1f} segundos")
27
 
 
35
  audio_envelope_zoom = audio_envelope * 0.2 + 0.9 # Para zoom
36
  audio_envelope_wave = audio_envelope * (img_h // 6) # Para waveform
37
 
38
+ # 4. Generar frames con zoom y waveform
39
  def make_frame(t):
40
+ # Calcular posici贸n en el audio
41
  time_idx = int(t * sr)
42
+
43
+ # --- Efecto de Zoom ---
44
  zoom_factor = audio_envelope_zoom[time_idx] if time_idx < len(audio_envelope_zoom) else 1.0
45
  new_size = (int(img_w * zoom_factor), int(img_h * zoom_factor))
46
  zoomed_img = img.resize(new_size, Image.LANCZOS)
 
55
  y_offset + img_h
56
  ))
57
 
58
+ # --- Dibujar Waveform ---
59
  frame = ImageDraw.Draw(cropped_img)
60
  start_y = int(img_h * 0.8) # 80% hacia abajo
61
 
 
73
  points.extend([(x, y_pos), (x, y_neg)])
74
 
75
  if len(points) > 2:
76
+ frame.polygon(points, fill=(255, 0, 0, 150)) # Rojo semitransparente
77
 
78
  return np.array(cropped_img)
79
 
 
82
  video.fps = 24
83
  video = video.set_audio(mp.AudioFileClip(audio_file))
84
 
85
+ # 6. Guardar video en un directorio temporal persistente
86
  temp_dir = tempfile.mkdtemp()
87
  output_path = os.path.join(temp_dir, "output.mp4")
88
 
89
+ logger.info(f"Exportando video a: {output_path}")
90
  video.write_videofile(
91
  output_path,
92
  codec="libx264",
 
95
  logger=None
96
  )
97
 
98
+ # Verificar que el archivo existe
99
+ if not os.path.exists(output_path):
100
+ raise Exception("Error: El archivo de video no se gener贸 correctamente.")
101
+
102
+ logger.info(f"Video guardado correctamente: {output_path}")
103
  return output_path # Retornar la ruta completa
104
 
105
  except Exception as e:
106
+ logger.error(f"Error cr铆tico: {str(e)}", exc_info=True)
107
+ return None
108
 
109
  # Interfaz Gradio
110
  iface = gr.Interface(
 
119
  )
120
 
121
  if __name__ == "__main__":
122
+ iface.queue(max_size=1).launch(show_error=True)