Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,43 +7,71 @@ 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 |
-
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# Create a directory to save the frames
|
| 23 |
os.makedirs('frames', exist_ok=True)
|
| 24 |
|
| 25 |
-
# Generate and save
|
| 26 |
-
for i
|
| 27 |
plt.figure(figsize=(10, 6))
|
| 28 |
-
|
| 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 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
video_path = 'output_video.mp4'
|
| 44 |
-
clip.write_videofile(video_path, codec='libx264')
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Gradio interface function
|
| 49 |
def process_audio(audio):
|
|
@@ -52,13 +80,20 @@ def process_audio(audio):
|
|
| 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(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
|
|
@@ -71,9 +106,8 @@ if __name__ == "__main__":
|
|
| 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
|
|
|
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
import librosa
|
| 9 |
import librosa.display
|
|
|
|
| 10 |
import os
|
| 11 |
import moviepy.video.io.ImageSequenceClip
|
| 12 |
|
| 13 |
# Function to generate frequency visualization frames from audio
|
| 14 |
def generate_frequency_visualization(audio_path):
|
| 15 |
+
try:
|
| 16 |
+
# Load the audio file
|
| 17 |
+
y, sr = librosa.load(audio_path, sr=None)
|
| 18 |
|
| 19 |
+
if sr == 0 or len(y) == 0:
|
| 20 |
+
raise ValueError("Invalid audio file: sampling rate or audio data is zero.")
|
| 21 |
|
| 22 |
+
# Perform Short-Time Fourier Transform (STFT)
|
| 23 |
+
D = librosa.amplitude_to_db(np.abs(librosa.stft(y)), ref=np.max)
|
| 24 |
+
|
| 25 |
+
# Create a directory to save the frames
|
| 26 |
+
os.makedirs('frames', exist_ok=True)
|
| 27 |
+
|
| 28 |
+
# Generate and save each frame
|
| 29 |
+
for i, frame in enumerate(D.T):
|
| 30 |
+
plt.figure(figsize=(10, 6))
|
| 31 |
+
librosa.display.specshow(frame.reshape(1, -1), sr=sr, x_axis='time', y_axis='log')
|
| 32 |
+
plt.axis('off')
|
| 33 |
+
plt.savefig(f'frames/frame_{i:04d}.png', bbox_inches='tight', pad_inches=0)
|
| 34 |
+
plt.close()
|
| 35 |
+
|
| 36 |
+
return 'frames'
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"Error generating frequency visualization: {e}")
|
| 39 |
+
# Fallback: Generate a default visualization
|
| 40 |
+
generate_default_visualization()
|
| 41 |
+
return 'frames'
|
| 42 |
+
|
| 43 |
+
# Function to generate a default visualization
|
| 44 |
+
def generate_default_visualization():
|
| 45 |
# Create a directory to save the frames
|
| 46 |
os.makedirs('frames', exist_ok=True)
|
| 47 |
|
| 48 |
+
# Generate and save default frames
|
| 49 |
+
for i in range(10): # Generate 10 default frames
|
| 50 |
plt.figure(figsize=(10, 6))
|
| 51 |
+
plt.plot(np.sin(np.linspace(0, 10, 100)) * (i + 1))
|
| 52 |
plt.axis('off')
|
| 53 |
plt.savefig(f'frames/frame_{i:04d}.png', bbox_inches='tight', pad_inches=0)
|
| 54 |
plt.close()
|
| 55 |
|
|
|
|
|
|
|
| 56 |
# Function to create a video from the generated frames
|
| 57 |
def create_video_from_frames(frames_directory):
|
| 58 |
+
try:
|
| 59 |
+
# Get the list of frame files
|
| 60 |
+
frame_files = [os.path.join(frames_directory, f) for f in os.listdir(frames_directory) if f.endswith('.png')]
|
| 61 |
+
frame_files.sort()
|
| 62 |
|
| 63 |
+
if not frame_files:
|
| 64 |
+
raise ValueError("No frames found to create the video.")
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
# Create a video from the frames
|
| 67 |
+
clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(frame_files, fps=30)
|
| 68 |
+
video_path = 'output_video.mp4'
|
| 69 |
+
clip.write_videofile(video_path, codec='libx264')
|
| 70 |
+
|
| 71 |
+
return video_path
|
| 72 |
+
except Exception as e:
|
| 73 |
+
print(f"Error creating video from frames: {e}")
|
| 74 |
+
return None
|
| 75 |
|
| 76 |
# Gradio interface function
|
| 77 |
def process_audio(audio):
|
|
|
|
| 80 |
video_path = create_video_from_frames(frames_directory)
|
| 81 |
return video_path
|
| 82 |
|
| 83 |
+
# Create the Gradio interface with explanations and recommendations
|
| 84 |
iface = gr.Interface(
|
| 85 |
fn=process_audio,
|
| 86 |
+
inputs=gr.Audio(type="filepath", label="Upload Audio File"),
|
| 87 |
outputs=gr.Video(label="Generated Video"),
|
| 88 |
title="Audio Frequency Visualization",
|
| 89 |
+
description="Upload an audio file to generate a video with frequency visualization. "
|
| 90 |
+
"Supported file types: WAV, MP3, FLAC. "
|
| 91 |
+
"Recommended file duration: 10 seconds to 5 minutes. "
|
| 92 |
+
"If the file is invalid or cannot be processed, a default visualization will be generated.",
|
| 93 |
+
examples=[
|
| 94 |
+
["examples/sample_audio.wav"],
|
| 95 |
+
["examples/sample_audio.mp3"]
|
| 96 |
+
]
|
| 97 |
)
|
| 98 |
|
| 99 |
# Launch the Gradio interface
|
|
|
|
| 106 |
# - librosa
|
| 107 |
# - numpy
|
| 108 |
# - matplotlib
|
|
|
|
| 109 |
# - moviepy
|
| 110 |
# - gradio
|
| 111 |
#
|
| 112 |
# You can install these dependencies using pip:
|
| 113 |
+
# pip install librosa numpy matplotlib moviepy gradio
|