|
1 |
| -# Coded with the help of Microsoft CoPilot |
2 |
| - |
3 | 1 | import os
|
4 | 2 | import subprocess
|
5 | 3 | import progressbar
|
6 |
| -from concurrent.futures import ThreadPoolExecutor |
7 | 4 | from nulrdcscripts.tools.spectrogramgeneration.params import args
|
8 | 5 |
|
9 | 6 |
|
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""" |
12 | 9 | 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)) |
44 | 11 | 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 ***") |
62 | 31 | bar.finish()
|
63 | 32 |
|
64 | 33 |
|
@@ -103,9 +72,7 @@ def checkOrCreateOutput(output_path, input_path):
|
103 | 72 | def callableSpectrogram(ffprobe_path, input_path, output_path, ffmpeg_path):
|
104 | 73 | channels = getnumberchannels(ffprobe_path, input_path)
|
105 | 74 | 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) |
109 | 76 |
|
110 | 77 |
|
111 | 78 | def main():
|
|
0 commit comments