# app.py # ============= # 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. import gradio as gr import numpy as np import matplotlib.pyplot as plt import librosa import librosa.display import cv2 import os import moviepy.video.io.ImageSequenceClip # Function to generate frequency visualization frames from audio def generate_frequency_visualization(audio_path): # Load the audio file y, sr = librosa.load(audio_path) # Perform Short-Time Fourier Transform (STFT) D = librosa.amplitude_to_db(np.abs(librosa.stft(y)), ref=np.max) # Create a directory to save the frames os.makedirs('frames', exist_ok=True) # Generate and save each frame for i, frame in enumerate(D.T): plt.figure(figsize=(10, 6)) librosa.display.specshow(frame.reshape(1, -1), sr=sr, x_axis='time', y_axis='log') plt.axis('off') plt.savefig(f'frames/frame_{i:04d}.png', bbox_inches='tight', pad_inches=0) plt.close() return 'frames' # Function to create a video from the generated frames def create_video_from_frames(frames_directory): # Get the list of frame files frame_files = [os.path.join(frames_directory, f) for f in os.listdir(frames_directory) if f.endswith('.png')] frame_files.sort() # Create a video from the frames clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(frame_files, fps=30) video_path = 'output_video.mp4' clip.write_videofile(video_path, codec='libx264') return video_path # Gradio interface function def process_audio(audio): audio_path = audio frames_directory = generate_frequency_visualization(audio_path) video_path = create_video_from_frames(frames_directory) return video_path # Create the Gradio interface iface = gr.Interface( fn=process_audio, inputs=gr.Audio(source="upload", type="filepath"), outputs=gr.Video(label="Generated Video"), title="Audio Frequency Visualization", description="Upload an audio file to generate a video with frequency visualization." ) # Launch the Gradio interface if __name__ == "__main__": iface.launch() # Dependencies # ============= # The following dependencies are required to run this app: # - librosa # - numpy # - matplotlib # - opencv-python # - moviepy # - gradio # # You can install these dependencies using pip: # pip install librosa numpy matplotlib opencv-python moviepy gradio