Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -13,6 +13,7 @@ import tempfile
|
|
13 |
import ffmpeg
|
14 |
import subprocess
|
15 |
import traceback
|
|
|
16 |
|
17 |
path = "" # Update with your path
|
18 |
|
@@ -44,6 +45,11 @@ def getTrigger(ad: int, a: list, max: int = 1024) -> int:
|
|
44 |
|
45 |
def extract_cover_image(mp3_file):
|
46 |
audio = MP3(mp3_file, ID3=ID3)
|
|
|
|
|
|
|
|
|
|
|
47 |
for tag in audio.tags.values():
|
48 |
if isinstance(tag, APIC):
|
49 |
image_data = tag.data
|
@@ -103,12 +109,12 @@ def filecount(p):
|
|
103 |
return file_count
|
104 |
|
105 |
def render_frame(params):
|
106 |
-
n, samples_array, cover_img, title, artist, dominant_color, width, height, fps, name, oscres = params
|
107 |
-
num_frames = len(samples_array) // (
|
108 |
img = Image.new('RGB', (width, height), normalizeColour(dominant_color))
|
109 |
d = ImageDraw.Draw(img)
|
110 |
|
111 |
-
s = (
|
112 |
if s > len(samples_array):
|
113 |
return
|
114 |
e = center_to_top_left(getRenderCords(samples_array, getTrigger(s, samples_array, max=oscres),res=oscres,size=(width, height)), width=width, height=height)
|
@@ -145,12 +151,12 @@ def RenderVid(af, n, fps=30):
|
|
145 |
)
|
146 |
gr.Interface.download(f"{n}.mp4")
|
147 |
|
148 |
-
def main(file, name, fps=30, res: tuple=(1280,720), oscres=512):
|
149 |
global iii
|
150 |
iii = 0
|
151 |
# Load the audio file
|
152 |
audio_path = file
|
153 |
-
y, sr = librosa.load(audio_path, sr=
|
154 |
y_u8 = (y * 128 + 128).astype('uint8')
|
155 |
samples_array = y_u8.tolist()
|
156 |
|
@@ -168,15 +174,15 @@ def main(file, name, fps=30, res: tuple=(1280,720), oscres=512):
|
|
168 |
|
169 |
# Frame rendering parameters
|
170 |
width, height, fps = res[0], res[1], fps
|
171 |
-
num_frames = len(samples_array) // (
|
172 |
|
173 |
# Prepare parameters for each frame
|
174 |
-
params = [(n, samples_array, cover_img, title, artist, dominant_color, width, height, fps, name, oscres) for n in range(num_frames)]
|
175 |
p = gr.Progress()
|
176 |
try:
|
177 |
with Pool(cpu_count()) as pool:
|
178 |
|
179 |
-
num_frames = len(samples_array) // (
|
180 |
# Use imap to get progress updates
|
181 |
for _ in pool.imap_unordered(render_frame, params):
|
182 |
iii += 1 # Increment frame count for progress
|
@@ -203,10 +209,10 @@ def main(file, name, fps=30, res: tuple=(1280,720), oscres=512):
|
|
203 |
]
|
204 |
subprocess.run(ffmpeg_cmd)
|
205 |
|
206 |
-
def gradio_interface(audio_file, output_name, fps=30, vidwidth=1280, vidheight=720, oscres=512):
|
207 |
resolution = f"{vidwidth}x{vidheight}"
|
208 |
res = tuple(map(int, resolution.split('x')))
|
209 |
-
main(audio_file, output_name, fps=fps, res=res, oscres=oscres)
|
210 |
time.sleep(5)
|
211 |
return f"{output_name}.mp4"
|
212 |
|
@@ -219,7 +225,8 @@ iface = gr.Interface(
|
|
219 |
gr.components.Slider(label="Frames per Second", minimum=30, maximum=60, step=1, value=30),
|
220 |
gr.components.Slider(label="Output Video Width", minimum=100, maximum=2000, value=1280),
|
221 |
gr.components.Slider(label="Output Video Height", minimum=100, maximum=2000, value=720),
|
222 |
-
gr.components.Slider(label="Number of Visualization Segments", minimum=
|
|
|
223 |
],
|
224 |
outputs=gr.components.Video(label="Output"),
|
225 |
title="MP3 to Video Visualization",
|
|
|
13 |
import ffmpeg
|
14 |
import subprocess
|
15 |
import traceback
|
16 |
+
import time
|
17 |
|
18 |
path = "" # Update with your path
|
19 |
|
|
|
45 |
|
46 |
def extract_cover_image(mp3_file):
|
47 |
audio = MP3(mp3_file, ID3=ID3)
|
48 |
+
if audio == None:
|
49 |
+
gr.Error()
|
50 |
+
raise("Missing MP3 Tag")
|
51 |
+
gr.Error("Mp3 is missing tags")
|
52 |
+
return None
|
53 |
for tag in audio.tags.values():
|
54 |
if isinstance(tag, APIC):
|
55 |
image_data = tag.data
|
|
|
109 |
return file_count
|
110 |
|
111 |
def render_frame(params):
|
112 |
+
n, samples_array, cover_img, title, artist, dominant_color, width, height, fps, name, oscres, sr = params
|
113 |
+
num_frames = len(samples_array) // (sr // fps)
|
114 |
img = Image.new('RGB', (width, height), normalizeColour(dominant_color))
|
115 |
d = ImageDraw.Draw(img)
|
116 |
|
117 |
+
s = (sr // fps) * n
|
118 |
if s > len(samples_array):
|
119 |
return
|
120 |
e = center_to_top_left(getRenderCords(samples_array, getTrigger(s, samples_array, max=oscres),res=oscres,size=(width, height)), width=width, height=height)
|
|
|
151 |
)
|
152 |
gr.Interface.download(f"{n}.mp4")
|
153 |
|
154 |
+
def main(file, name, fps=30, res: tuple=(1280,720), oscres=512, sr=11025):
|
155 |
global iii
|
156 |
iii = 0
|
157 |
# Load the audio file
|
158 |
audio_path = file
|
159 |
+
y, sr = librosa.load(audio_path, sr=sr) # Resample to 11025 Hz
|
160 |
y_u8 = (y * 128 + 128).astype('uint8')
|
161 |
samples_array = y_u8.tolist()
|
162 |
|
|
|
174 |
|
175 |
# Frame rendering parameters
|
176 |
width, height, fps = res[0], res[1], fps
|
177 |
+
num_frames = len(samples_array) // (sr // fps)
|
178 |
|
179 |
# Prepare parameters for each frame
|
180 |
+
params = [(n, samples_array, cover_img, title, artist, dominant_color, width, height, fps, name, oscres, sr) for n in range(num_frames)]
|
181 |
p = gr.Progress()
|
182 |
try:
|
183 |
with Pool(cpu_count()) as pool:
|
184 |
|
185 |
+
num_frames = len(samples_array) // (sr // fps)
|
186 |
# Use imap to get progress updates
|
187 |
for _ in pool.imap_unordered(render_frame, params):
|
188 |
iii += 1 # Increment frame count for progress
|
|
|
209 |
]
|
210 |
subprocess.run(ffmpeg_cmd)
|
211 |
|
212 |
+
def gradio_interface(audio_file, output_name, fps=30, vidwidth=1280, vidheight=720, oscres=512, sr=11025):
|
213 |
resolution = f"{vidwidth}x{vidheight}"
|
214 |
res = tuple(map(int, resolution.split('x')))
|
215 |
+
main(audio_file, output_name, fps=fps, res=res, oscres=oscres, sr=sr)
|
216 |
time.sleep(5)
|
217 |
return f"{output_name}.mp4"
|
218 |
|
|
|
225 |
gr.components.Slider(label="Frames per Second", minimum=30, maximum=60, step=1, value=30),
|
226 |
gr.components.Slider(label="Output Video Width", minimum=100, maximum=2000, value=1280),
|
227 |
gr.components.Slider(label="Output Video Height", minimum=100, maximum=2000, value=720),
|
228 |
+
gr.components.Slider(label="Number of Visualization Segments", minimum=256, maximum=2048, step=2, value=512)
|
229 |
+
gr.components.Slider(label="Scope Sample Rate", minimum=11025, maximum=44100, step=2, value=11025)
|
230 |
],
|
231 |
outputs=gr.components.Video(label="Output"),
|
232 |
title="MP3 to Video Visualization",
|