Spaces:
Sleeping
Sleeping
File size: 2,496 Bytes
7530063 4654b73 7530063 4654b73 7530063 4654b73 7530063 4654b73 7530063 4654b73 7530063 4654b73 7530063 4654b73 7530063 4654b73 7530063 4654b73 7530063 4654b73 7530063 4654b73 7530063 4654b73 7530063 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# 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
|