marquesafonso commited on
Commit
2f40a85
·
1 Parent(s): 6ec9b2f

refactor for video output

Browse files
Files changed (1) hide show
  1. app.py +9 -7
app.py CHANGED
@@ -4,6 +4,8 @@ import matplotlib.pyplot as plt
4
  from matplotlib.animation import FuncAnimation
5
  import io
6
  import librosa
 
 
7
 
8
  def extract_waveform_animation(audio_file, duration):
9
  # Load audio file
@@ -32,12 +34,12 @@ def extract_waveform_animation(audio_file, duration):
32
  # Create the animation
33
  ani = FuncAnimation(fig, update, frames=np.arange(0, num_frames, sr // 10), init_func=init, blit=True)
34
 
35
- # Save the animation to a buffer
36
- buf = io.BytesIO()
37
- ani.save(buf, format='gif')
38
- buf.seek(0)
39
 
40
- return buf
41
 
42
  # Define the Gradio interface
43
  iface = gr.Interface(
@@ -46,8 +48,8 @@ iface = gr.Interface(
46
  gr.Audio(type="filepath"),
47
  gr.Number(label="Duration (seconds)", value=5)
48
  ],
49
- outputs=gr.Image(type="file", format="gif"),
50
- description="Upload an audio file to extract an animation from its waveform. Specify the duration of the animation."
51
  )
52
 
53
  # Launch the app
 
4
  from matplotlib.animation import FuncAnimation
5
  import io
6
  import librosa
7
+ import tempfile
8
+ import os
9
 
10
  def extract_waveform_animation(audio_file, duration):
11
  # Load audio file
 
34
  # Create the animation
35
  ani = FuncAnimation(fig, update, frames=np.arange(0, num_frames, sr // 10), init_func=init, blit=True)
36
 
37
+ # Save the animation to a temporary file
38
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
39
+ ani.save(tmpfile.name, writer='ffmpeg', fps=30)
40
+ video_path = tmpfile.name
41
 
42
+ return video_path
43
 
44
  # Define the Gradio interface
45
  iface = gr.Interface(
 
48
  gr.Audio(type="filepath"),
49
  gr.Number(label="Duration (seconds)", value=5)
50
  ],
51
+ outputs=gr.Video(),
52
+ description="Upload an audio file to extract a video animation from its waveform. Specify the duration of the animation."
53
  )
54
 
55
  # Launch the app