gnosticdev commited on
Commit
000e078
verified
1 Parent(s): 7377045

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -15
app.py CHANGED
@@ -10,18 +10,14 @@ import io
10
 
11
  def generate_waveform_video(audio_file, image_file):
12
  try:
13
- # Validar que los archivos existan
14
- if not os.path.exists(audio_file):
15
- raise ValueError("Archivo de audio no encontrado")
16
- if not os.path.exists(image_file):
17
- raise ValueError("Archivo de imagen no encontrado")
18
-
19
  # 1. Cargar audio
20
  y, sr = librosa.load(audio_file)
21
  duration = librosa.get_duration(y=y, sr=sr)
22
 
23
- # 2. Cargar imagen
24
- img_clip = mp.ImageClip(image_file).set_duration(duration)
 
 
25
  img_w, img_h = img_clip.size
26
 
27
  # 3. Crear efecto de waveform
@@ -30,6 +26,9 @@ def generate_waveform_video(audio_file, image_file):
30
 
31
  def make_frame(t):
32
  fig, ax = plt.subplots(figsize=(img_w/100, img_h/100), dpi=100)
 
 
 
33
  ax.set_xlim(0, duration)
34
  ax.set_ylim(-img_h//2, img_h//2)
35
  ax.axis('off')
@@ -43,11 +42,15 @@ def generate_waveform_video(audio_file, image_file):
43
  ax.fill_between(x, wave_slice - img_h//4, -wave_slice + img_h//4,
44
  facecolor='red', alpha=0.7)
45
 
46
- # Convertir figura a numpy array usando io.BytesIO
47
  buf = io.BytesIO()
48
- plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0, transparent=False)
 
49
  plt.close(fig)
50
- return np.array(Image.open(buf))
 
 
 
51
 
52
  # Crear video
53
  effect_clip = mp.VideoClip(make_frame, duration=duration).set_fps(24)
@@ -61,8 +64,7 @@ def generate_waveform_video(audio_file, image_file):
61
  return tmpfile.name
62
 
63
  except Exception as e:
64
- # Lanzar excepci贸n para que Gradio muestre el error
65
- raise Exception(f"Error durante la generaci贸n: {str(e)}")
66
 
67
  # Interfaz Gradio
68
  iface = gr.Interface(
@@ -72,8 +74,8 @@ iface = gr.Interface(
72
  gr.Image(type="filepath", label="Imagen de Fondo"),
73
  ],
74
  outputs=gr.Video(label="Video Resultante", format="mp4"),
75
- title="Generador de Video con Efectos de Audio",
76
- description="Crea videos con efectos visuales sincronizados con el audio. Actualmente soporta efecto de waveform."
77
  )
78
 
79
  if __name__ == "__main__":
 
10
 
11
  def generate_waveform_video(audio_file, image_file):
12
  try:
 
 
 
 
 
 
13
  # 1. Cargar audio
14
  y, sr = librosa.load(audio_file)
15
  duration = librosa.get_duration(y=y, sr=sr)
16
 
17
+ # 2. Cargar imagen y asegurar formato RGB
18
+ img = Image.open(image_file).convert('RGB') # Eliminar cualquier transparencia
19
+ img_array = np.array(img)
20
+ img_clip = mp.ImageClip(img_array).set_duration(duration)
21
  img_w, img_h = img_clip.size
22
 
23
  # 3. Crear efecto de waveform
 
26
 
27
  def make_frame(t):
28
  fig, ax = plt.subplots(figsize=(img_w/100, img_h/100), dpi=100)
29
+ fig.patch.set_facecolor('black') # Fondo s贸lido
30
+ ax.set_facecolor('black')
31
+
32
  ax.set_xlim(0, duration)
33
  ax.set_ylim(-img_h//2, img_h//2)
34
  ax.axis('off')
 
42
  ax.fill_between(x, wave_slice - img_h//4, -wave_slice + img_h//4,
43
  facecolor='red', alpha=0.7)
44
 
45
+ # Convertir a RGB sin transparencia
46
  buf = io.BytesIO()
47
+ plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0,
48
+ transparent=False, facecolor='black')
49
  plt.close(fig)
50
+
51
+ # Forzar conversi贸n a RGB
52
+ img_frame = Image.open(buf).convert('RGB')
53
+ return np.array(img_frame)
54
 
55
  # Crear video
56
  effect_clip = mp.VideoClip(make_frame, duration=duration).set_fps(24)
 
64
  return tmpfile.name
65
 
66
  except Exception as e:
67
+ raise Exception(f"Error: {str(e)}")
 
68
 
69
  # Interfaz Gradio
70
  iface = gr.Interface(
 
74
  gr.Image(type="filepath", label="Imagen de Fondo"),
75
  ],
76
  outputs=gr.Video(label="Video Resultante", format="mp4"),
77
+ title="Generador de Video Musical",
78
+ description="Crea videos con efectos de onda de audio sincronizados. Usa im谩genes JPG/PNG y audio WAV/MP3."
79
  )
80
 
81
  if __name__ == "__main__":