Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,68 +3,57 @@ import moviepy.editor as mp
|
|
3 |
import numpy as np
|
4 |
import librosa
|
5 |
import matplotlib.pyplot as plt
|
6 |
-
from
|
7 |
-
import
|
|
|
8 |
|
9 |
-
|
10 |
-
logging.basicConfig(level=logging.INFO)
|
11 |
-
logger = logging.getLogger("audio_to_video")
|
12 |
-
|
13 |
-
def generate_waveform_video(audio_path, image_path):
|
14 |
try:
|
15 |
# 1. Cargar audio
|
16 |
-
|
17 |
-
y, sr = librosa.load(audio_path)
|
18 |
duration = librosa.get_duration(y=y, sr=sr)
|
19 |
-
logger.info(f"Duraci贸n del audio: {duration:.2f} segundos")
|
20 |
|
21 |
# 2. Cargar imagen
|
22 |
-
|
23 |
-
|
24 |
-
img_width, img_height = img_clip.size
|
25 |
|
26 |
# 3. Crear efecto de waveform
|
27 |
-
|
28 |
-
audio_envelope = np.
|
29 |
-
audio_envelope = (audio_envelope / np.max(audio_envelope)) * (img_height // 3)
|
30 |
|
31 |
def make_frame(t):
|
32 |
-
fig, ax = plt.subplots(figsize=(
|
33 |
ax.set_xlim(0, duration)
|
34 |
-
ax.set_ylim(-
|
35 |
ax.axis('off')
|
36 |
-
|
37 |
-
|
38 |
-
start = max(0,
|
39 |
-
end = min(len(audio_envelope),
|
40 |
wave_slice = audio_envelope[start:end]
|
41 |
-
|
42 |
-
|
43 |
-
ax.fill_between(
|
44 |
facecolor='red', alpha=0.7)
|
45 |
-
|
46 |
-
|
|
|
47 |
plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0)
|
48 |
plt.close(fig)
|
49 |
-
return
|
50 |
|
51 |
-
|
52 |
effect_clip = mp.VideoClip(make_frame, duration=duration).set_fps(24)
|
53 |
final_clip = mp.CompositeVideoClip([img_clip, effect_clip.set_pos("center")])
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
audio_codec="aac", logger=None)
|
62 |
-
buffer.seek(0)
|
63 |
-
logger.info("Video generado exitosamente")
|
64 |
-
return buffer
|
65 |
|
66 |
except Exception as e:
|
67 |
-
logger.error(f"Error durante la generaci贸n: {str(e)}")
|
68 |
return f"Error: {str(e)}"
|
69 |
|
70 |
# Interfaz Gradio
|
@@ -76,10 +65,8 @@ iface = gr.Interface(
|
|
76 |
],
|
77 |
outputs=gr.Video(label="Video Resultante", format="mp4"),
|
78 |
title="Generador de Video con Efectos de Audio",
|
79 |
-
description="Crea videos con efectos visuales sincronizados con el audio. Actualmente soporta efecto de waveform."
|
80 |
-
allow_flagging="never"
|
81 |
)
|
82 |
|
83 |
if __name__ == "__main__":
|
84 |
-
|
85 |
-
iface.queue().launch(share=False, debug=True)
|
|
|
3 |
import numpy as np
|
4 |
import librosa
|
5 |
import matplotlib.pyplot as plt
|
6 |
+
from PIL import Image
|
7 |
+
import tempfile
|
8 |
+
import os
|
9 |
|
10 |
+
def generate_waveform_video(audio_file, image_file):
|
|
|
|
|
|
|
|
|
11 |
try:
|
12 |
# 1. Cargar audio
|
13 |
+
y, sr = librosa.load(audio_file)
|
|
|
14 |
duration = librosa.get_duration(y=y, sr=sr)
|
|
|
15 |
|
16 |
# 2. Cargar imagen
|
17 |
+
img_clip = mp.ImageClip(image_file).set_duration(duration)
|
18 |
+
img_w, img_h = img_clip.size
|
|
|
19 |
|
20 |
# 3. Crear efecto de waveform
|
21 |
+
audio_envelope = np.abs(y)
|
22 |
+
audio_envelope = (audio_envelope / np.max(audio_envelope)) * (img_h // 3)
|
|
|
23 |
|
24 |
def make_frame(t):
|
25 |
+
fig, ax = plt.subplots(figsize=(img_w/100, img_h/100), dpi=100)
|
26 |
ax.set_xlim(0, duration)
|
27 |
+
ax.set_ylim(-img_h//2, img_h//2)
|
28 |
ax.axis('off')
|
29 |
+
|
30 |
+
time_idx = int(t * sr)
|
31 |
+
start = max(0, time_idx - sr//10)
|
32 |
+
end = min(len(audio_envelope), time_idx + sr//10)
|
33 |
wave_slice = audio_envelope[start:end]
|
34 |
+
|
35 |
+
x = np.linspace(t-0.1, t+0.1, len(wave_slice))
|
36 |
+
ax.fill_between(x, wave_slice - img_h//4, -wave_slice + img_h//4,
|
37 |
facecolor='red', alpha=0.7)
|
38 |
+
|
39 |
+
# Convertir figura a numpy array
|
40 |
+
buf = io.BytesIO()
|
41 |
plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0)
|
42 |
plt.close(fig)
|
43 |
+
return np.array(Image.open(buf)) # Formato correcto para MoviePy
|
44 |
|
45 |
+
# Crear video
|
46 |
effect_clip = mp.VideoClip(make_frame, duration=duration).set_fps(24)
|
47 |
final_clip = mp.CompositeVideoClip([img_clip, effect_clip.set_pos("center")])
|
48 |
+
final_clip = final_clip.set_audio(mp.AudioFileClip(audio_file))
|
49 |
+
|
50 |
+
# Guardar en archivo temporal
|
51 |
+
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
|
52 |
+
final_clip.write_videofile(tmpfile.name, fps=24, codec="libx264",
|
53 |
+
audio_codec="aac", logger=None)
|
54 |
+
return tmpfile.name
|
|
|
|
|
|
|
|
|
55 |
|
56 |
except Exception as e:
|
|
|
57 |
return f"Error: {str(e)}"
|
58 |
|
59 |
# Interfaz Gradio
|
|
|
65 |
],
|
66 |
outputs=gr.Video(label="Video Resultante", format="mp4"),
|
67 |
title="Generador de Video con Efectos de Audio",
|
68 |
+
description="Crea videos con efectos visuales sincronizados con el audio. Actualmente soporta efecto de waveform."
|
|
|
69 |
)
|
70 |
|
71 |
if __name__ == "__main__":
|
72 |
+
iface.queue().launch()
|
|