Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ import matplotlib.pyplot as plt
|
|
8 |
import librosa
|
9 |
import librosa.display
|
10 |
import os
|
11 |
-
import
|
12 |
|
13 |
# Function to generate frequency visualization frames from audio
|
14 |
def generate_frequency_visualization(audio_path, fps, num_bars):
|
@@ -41,10 +41,10 @@ def generate_frequency_visualization(audio_path, fps, num_bars):
|
|
41 |
|
42 |
# Generate and save each frame
|
43 |
for i, heights in enumerate(bar_heights):
|
44 |
-
plt.
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
plt.savefig(f'frames/frame_{i:04d}.png', bbox_inches='tight', pad_inches=0)
|
49 |
plt.close()
|
50 |
|
@@ -64,17 +64,27 @@ def create_video_from_frames(frames_directory, audio_path, fps, duration):
|
|
64 |
if not frame_files:
|
65 |
raise ValueError("No frames found to create the video.")
|
66 |
|
67 |
-
#
|
68 |
-
|
|
|
|
|
|
|
69 |
video_path = 'output_video.mp4'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
|
76 |
print(f"Video created with {len(frame_files)} frames.")
|
77 |
-
return
|
78 |
except Exception as e:
|
79 |
print(f"Error creating video from frames: {e}")
|
80 |
return None
|
@@ -113,4 +123,5 @@ if __name__ == "__main__":
|
|
113 |
# - librosa
|
114 |
# - numpy
|
115 |
# - matplotlib
|
116 |
-
# -
|
|
|
|
8 |
import librosa
|
9 |
import librosa.display
|
10 |
import os
|
11 |
+
import cv2
|
12 |
|
13 |
# Function to generate frequency visualization frames from audio
|
14 |
def generate_frequency_visualization(audio_path, fps, num_bars):
|
|
|
41 |
|
42 |
# Generate and save each frame
|
43 |
for i, heights in enumerate(bar_heights):
|
44 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
45 |
+
ax.bar(range(num_bars), heights, color=plt.cm.viridis(np.linspace(0, 1, num_bars)))
|
46 |
+
ax.set_ylim(0, np.max(S))
|
47 |
+
ax.axis('off')
|
48 |
plt.savefig(f'frames/frame_{i:04d}.png', bbox_inches='tight', pad_inches=0)
|
49 |
plt.close()
|
50 |
|
|
|
64 |
if not frame_files:
|
65 |
raise ValueError("No frames found to create the video.")
|
66 |
|
67 |
+
# Get video dimensions from the first frame
|
68 |
+
first_frame = cv2.imread(frame_files[0])
|
69 |
+
height, width, _ = first_frame.shape
|
70 |
+
|
71 |
+
# Initialize video writer
|
72 |
video_path = 'output_video.mp4'
|
73 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
74 |
+
video_writer = cv2.VideoWriter(video_path, fourcc, fps, (width, height))
|
75 |
+
|
76 |
+
# Write frames to video
|
77 |
+
for frame_file in frame_files:
|
78 |
+
frame = cv2.imread(frame_file)
|
79 |
+
video_writer.write(frame)
|
80 |
|
81 |
+
video_writer.release()
|
82 |
+
|
83 |
+
# Merge audio with video using ffmpeg
|
84 |
+
os.system(f"ffmpeg -i {video_path} -i {audio_path} -c:v copy -c:a aac -strict experimental output_with_audio.mp4 -y")
|
85 |
|
86 |
print(f"Video created with {len(frame_files)} frames.")
|
87 |
+
return 'output_with_audio.mp4'
|
88 |
except Exception as e:
|
89 |
print(f"Error creating video from frames: {e}")
|
90 |
return None
|
|
|
123 |
# - librosa
|
124 |
# - numpy
|
125 |
# - matplotlib
|
126 |
+
# - opencv-python
|
127 |
+
# - ffmpeg (installed separately)
|