Skip to content

Commit f91712a

Browse files
committed
multi-thread didn't work so update to go back
1 parent d09ec34 commit f91712a

File tree

1 file changed

+23
-56
lines changed
  • nulrdcscripts/tools/spectrogramgeneration

1 file changed

+23
-56
lines changed

nulrdcscripts/tools/spectrogramgeneration/main.py

Lines changed: 23 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,33 @@
1-
# Coded with the help of Microsoft CoPilot
2-
31
import os
42
import subprocess
53
import progressbar
6-
from concurrent.futures import ThreadPoolExecutor
74
from nulrdcscripts.tools.spectrogramgeneration.params import args
85

96

10-
def generate_spectrogram(input_path, channel, output_path, spectroname, ffmpegpath):
11-
"""Creates a spectrogram for a single audio track in the input"""
7+
def generate_spectrogram(input_path, channels, output_path, spectroname, ffmpegpath):
8+
"""Creates a spectrogram for each audio track in the input"""
129
spectrogram_resolution = "1920x1080" # Updated resolution
13-
output = os.path.join(output_path, f"{spectroname}_spectrogram0{channel + 1}_s.png")
14-
spectrogram_args = [ffmpegpath]
15-
spectrogram_args += ["-loglevel", "error", "-y"]
16-
spectrogram_args += ["-i", input_path, "-lavfi"]
17-
if channel > 1:
18-
spectrogram_args += [
19-
f"[0:a:{channel}]showspectrumpic=mode=separate:s={spectrogram_resolution}"
20-
]
21-
else:
22-
spectrogram_args += [
23-
f"[0:a:{channel}]showspectrumpic=s={spectrogram_resolution}"
24-
]
25-
spectrogram_args += [output]
26-
subprocess.run(spectrogram_args)
27-
print(f"*** Spectrogram for channel {channel + 1} generated ***")
28-
29-
30-
def generate_all_spectrograms(
31-
input_path, channels, output_path, spectroname, ffmpegpath
32-
):
33-
"""Creates spectrograms for all audio channels in parallel"""
34-
widgets = [
35-
" [",
36-
progressbar.Percentage(),
37-
"] ",
38-
progressbar.Bar(),
39-
" (",
40-
progressbar.ETA(),
41-
") ",
42-
]
43-
bar = progressbar.ProgressBar(max_value=int(channels), widgets=widgets)
10+
bar = progressbar.ProgressBar(max_value=int(channels))
4411
bar.start()
45-
46-
with ThreadPoolExecutor(max_workers=int(channels)) as executor:
47-
futures = [
48-
executor.submit(
49-
generate_spectrogram,
50-
input_path,
51-
index,
52-
output_path,
53-
spectroname,
54-
ffmpegpath,
55-
)
56-
for index in range(int(channels))
57-
]
58-
for i, future in enumerate(futures):
59-
future.result()
60-
bar.update(i + 1)
61-
12+
for index in range(int(channels)):
13+
output = os.path.join(
14+
output_path, f"{spectroname}_spectrogram0{index + 1}_s.png"
15+
)
16+
spectrogram_args = [ffmpegpath]
17+
spectrogram_args += ["-loglevel", "error", "-y"]
18+
spectrogram_args += ["-i", input_path, "-lavfi"]
19+
if int(channels) > 1:
20+
spectrogram_args += [
21+
f"[0:a:{index}]showspectrumpic=mode=separate:s={spectrogram_resolution}"
22+
]
23+
else:
24+
spectrogram_args += [
25+
f"[0:a:{index}]showspectrumpic=s={spectrogram_resolution}"
26+
]
27+
spectrogram_args += [output]
28+
subprocess.run(spectrogram_args)
29+
bar.update(index + 1)
30+
print(f"*** Spectrogram for channel {index + 1} generated ***")
6231
bar.finish()
6332

6433

@@ -103,9 +72,7 @@ def checkOrCreateOutput(output_path, input_path):
10372
def callableSpectrogram(ffprobe_path, input_path, output_path, ffmpeg_path):
10473
channels = getnumberchannels(ffprobe_path, input_path)
10574
spectroname, _ = os.path.splitext(os.path.basename(input_path))
106-
generate_all_spectrograms(
107-
input_path, channels, output_path, spectroname, ffmpeg_path
108-
)
75+
generate_spectrogram(input_path, channels, output_path, spectroname, ffmpeg_path)
10976

11077

11178
def main():

0 commit comments

Comments
 (0)