Ivan000 commited on
Commit
7530063
·
verified ·
1 Parent(s): 5b29dbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -28
app.py CHANGED
@@ -1,49 +1,79 @@
 
 
 
 
1
  import gradio as gr
2
- import librosa
3
- import librosa.display
4
  import numpy as np
5
  import matplotlib.pyplot as plt
6
- from moviepy.editor import VideoClip, AudioFileClip, CompositeVideoClip
 
 
 
 
7
 
8
- def generate_frequency_visualization(audio_file):
 
9
  # Load the audio file
10
- y, sr = librosa.load(audio_file, sr=None)
11
 
12
- # Compute the Short-Time Fourier Transform (STFT)
13
  D = librosa.amplitude_to_db(np.abs(librosa.stft(y)), ref=np.max)
14
 
15
- # Create a figure for the visualization
16
- fig, ax = plt.subplots()
17
- img = librosa.display.specshow(D, sr=sr, ax=ax, y_axis='linear', fmax=8000)
18
- fig.colorbar(img, ax=ax, format="%+2.0f dB")
19
- ax.set(title='Frequency Visualization')
20
- plt.axis('off')
21
- plt.savefig('frequency_visualization.png', bbox_inches='tight', pad_inches=0, dpi=100)
22
- plt.close(fig)
23
 
24
- # Load the audio file
25
- audio_clip = AudioFileClip(audio_file)
 
 
 
 
 
26
 
27
- # Create a video clip from the frequency visualization image
28
- video_clip = VideoClip(lambda t: plt.imread('frequency_visualization.png'), duration=audio_clip.duration)
29
 
30
- # Combine the audio and video clips
31
- final_clip = video_clip.set_audio(audio_clip)
 
 
 
32
 
33
- # Write the final video to a file
34
- output_file = 'frequency_visualization.mp4'
35
- final_clip.write_videofile(output_file, codec='libx264', audio_codec='aac')
 
36
 
37
- return output_file
 
 
 
 
 
 
 
38
 
39
  # Create the Gradio interface
40
  iface = gr.Interface(
41
- fn=generate_frequency_visualization,
42
- inputs=gr.Audio(source="upload", type="file"),
43
- outputs=gr.Video(label="Frequency Visualization Video"),
44
  title="Audio Frequency Visualization",
45
  description="Upload an audio file to generate a video with frequency visualization."
46
  )
47
 
48
  # Launch the Gradio interface
49
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ # =============
3
+ # This is a complete app.py file for a Gradio application that allows users to upload an audio file and generate a video with frequency visualization.
4
+
5
  import gradio as gr
 
 
6
  import numpy as np
7
  import matplotlib.pyplot as plt
8
+ import librosa
9
+ import librosa.display
10
+ import cv2
11
+ import os
12
+ import moviepy.video.io.ImageSequenceClip
13
 
14
+ # Function to generate frequency visualization frames from audio
15
+ def generate_frequency_visualization(audio_path):
16
  # Load the audio file
17
+ y, sr = librosa.load(audio_path)
18
 
19
+ # Perform Short-Time Fourier Transform (STFT)
20
  D = librosa.amplitude_to_db(np.abs(librosa.stft(y)), ref=np.max)
21
 
22
+ # Create a directory to save the frames
23
+ os.makedirs('frames', exist_ok=True)
 
 
 
 
 
 
24
 
25
+ # Generate and save each frame
26
+ for i, frame in enumerate(D.T):
27
+ plt.figure(figsize=(10, 6))
28
+ librosa.display.specshow(frame.reshape(1, -1), sr=sr, x_axis='time', y_axis='log')
29
+ plt.axis('off')
30
+ plt.savefig(f'frames/frame_{i:04d}.png', bbox_inches='tight', pad_inches=0)
31
+ plt.close()
32
 
33
+ return 'frames'
 
34
 
35
+ # Function to create a video from the generated frames
36
+ def create_video_from_frames(frames_directory):
37
+ # Get the list of frame files
38
+ frame_files = [os.path.join(frames_directory, f) for f in os.listdir(frames_directory) if f.endswith('.png')]
39
+ frame_files.sort()
40
 
41
+ # Create a video from the frames
42
+ clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(frame_files, fps=30)
43
+ video_path = 'output_video.mp4'
44
+ clip.write_videofile(video_path, codec='libx264')
45
 
46
+ return video_path
47
+
48
+ # Gradio interface function
49
+ def process_audio(audio):
50
+ audio_path = audio
51
+ frames_directory = generate_frequency_visualization(audio_path)
52
+ video_path = create_video_from_frames(frames_directory)
53
+ return video_path
54
 
55
  # Create the Gradio interface
56
  iface = gr.Interface(
57
+ fn=process_audio,
58
+ inputs=gr.Audio(source="upload", type="filepath"),
59
+ outputs=gr.Video(label="Generated Video"),
60
  title="Audio Frequency Visualization",
61
  description="Upload an audio file to generate a video with frequency visualization."
62
  )
63
 
64
  # Launch the Gradio interface
65
+ if __name__ == "__main__":
66
+ iface.launch()
67
+
68
+ # Dependencies
69
+ # =============
70
+ # The following dependencies are required to run this app:
71
+ # - librosa
72
+ # - numpy
73
+ # - matplotlib
74
+ # - opencv-python
75
+ # - moviepy
76
+ # - gradio
77
+ #
78
+ # You can install these dependencies using pip:
79
+ # pip install librosa numpy matplotlib opencv-python moviepy gradio