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

add duration param

Browse files
Files changed (1) hide show
  1. app.py +10 -15
app.py CHANGED
@@ -5,15 +5,11 @@ 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
12
  y, sr = librosa.load(audio_file, sr=None)
13
-
14
- # Determine the number of frames needed for the specified duration
15
- num_frames = int(duration * sr)
16
- y = y[:num_frames]
17
 
18
  # Create a figure and axis for the animation
19
  fig, ax = plt.subplots()
@@ -28,15 +24,17 @@ def extract_waveform_animation(audio_file, duration):
28
 
29
  # Function to update the animation frame
30
  def update(frame):
31
- line.set_data(np.linspace(0, duration, num=len(y[:frame])), y[:frame])
 
 
32
  return line,
33
 
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
@@ -44,12 +42,9 @@ def extract_waveform_animation(audio_file, duration):
44
  # Define the Gradio interface
45
  iface = gr.Interface(
46
  fn=extract_waveform_animation,
47
- inputs=[
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
 
5
  import io
6
  import librosa
7
  import tempfile
 
8
 
9
+ def extract_waveform_animation(audio_file):
10
  # Load audio file
11
  y, sr = librosa.load(audio_file, sr=None)
12
+ duration = librosa.get_duration(y=y, sr=sr)
 
 
 
13
 
14
  # Create a figure and axis for the animation
15
  fig, ax = plt.subplots()
 
24
 
25
  # Function to update the animation frame
26
  def update(frame):
27
+ start = frame * sr
28
+ end = min(start + sr, len(y))
29
+ line.set_data(np.linspace(0, duration, num=len(y[:end])), y[:end])
30
  return line,
31
 
32
  # Create the animation
33
+ ani = FuncAnimation(fig, update, frames=np.arange(0, int(duration)), init_func=init, blit=True)
34
 
35
  # Save the animation to a temporary file
36
  with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
37
+ ani.save(tmpfile.name, writer='ffmpeg', fps=1)
38
  video_path = tmpfile.name
39
 
40
  return video_path
 
42
  # Define the Gradio interface
43
  iface = gr.Interface(
44
  fn=extract_waveform_animation,
45
+ inputs=gr.Audio(source="upload", type="file"),
46
+ outputs=gr.Video(type="file"),
47
+ description="Upload an audio file to extract a video animation from its waveform."
 
 
 
48
  )
49
 
50
  # Launch the app