|
1 |
| -import progressbar |
2 | 1 | import os
|
3 | 2 | import subprocess
|
| 3 | +import progressbar |
4 | 4 | from nulrdcscripts.tools.spectrogramgeneration.params import args
|
5 | 5 |
|
6 | 6 |
|
7 | 7 | def generate_spectrogram(input_path, channels, output_path, spectroname, ffmpegpath):
|
8 | 8 | """Creates a spectrogram for each audio track in the input"""
|
9 |
| - for index, item in enumerate(channels): |
10 |
| - spectrogram_resolution = "1928x1080" |
| 9 | + spectrogram_resolution = "1920x1080" # Updated resolution |
| 10 | + bar = progressbar.ProgressBar(max_value=int(channels)) |
| 11 | + bar.start() |
| 12 | + for index in range(int(channels)): |
11 | 13 | output = os.path.join(
|
12 |
| - output_path, spectroname + "_spectrogram0" + str(index + 1) + "_s.png" |
| 14 | + output_path, f"{spectroname}_spectrogram0{index + 1}_s.png" |
13 | 15 | )
|
14 | 16 | spectrogram_args = [ffmpegpath]
|
15 | 17 | spectrogram_args += ["-loglevel", "error", "-y"]
|
16 | 18 | spectrogram_args += ["-i", input_path, "-lavfi"]
|
17 |
| - item = int(item) |
18 |
| - if item > 1: |
| 19 | + if int(channels) > 1: |
19 | 20 | spectrogram_args += [
|
20 |
| - "[0:a:%(a)s]showspectrumpic=mode=separate:s=%(b)s" |
21 |
| - % {"a": index, "b": spectrogram_resolution} |
| 21 | + f"[0:a:{index}]showspectrumpic=mode=separate:s={spectrogram_resolution}" |
22 | 22 | ]
|
23 | 23 | else:
|
24 | 24 | spectrogram_args += [
|
25 |
| - "[0:a:%(a)s]showspectrumpic=s=%(b)s" |
26 |
| - % {"a": index, "b": spectrogram_resolution} |
| 25 | + f"[0:a:{index}]showspectrumpic=s={spectrogram_resolution}" |
27 | 26 | ]
|
28 | 27 | spectrogram_args += [output]
|
29 | 28 | subprocess.run(spectrogram_args)
|
30 |
| - print("*** Spectrogram Generated ***") |
| 29 | + bar.update(index + 1) |
| 30 | + print(f"*** Spectrogram for channel {index + 1} generated ***") |
| 31 | + bar.finish() |
31 | 32 |
|
32 | 33 |
|
33 | 34 | def softwarecheck(software, softwarename):
|
34 | 35 | """Checks that software exists"""
|
35 |
| - if "sox" in software: |
36 |
| - version = "--version" |
37 |
| - else: |
38 |
| - version = "-version" |
39 |
| - |
| 36 | + version = "--version" if "sox" in software else "-version" |
40 | 37 | try:
|
41 | 38 | subprocess.check_output([software, version]).decode(
|
42 | 39 | "ascii"
|
43 | 40 | ).rstrip().splitlines()[0].split()[2]
|
44 | 41 | except:
|
45 |
| - raise Exception("Cannot locate " + softwarename + ". Check path.") |
| 42 | + raise Exception(f"Cannot locate {softwarename}. Check path.") |
46 | 43 |
|
47 | 44 |
|
48 | 45 | def getnumberchannels(ffprobe_path, input_path):
|
49 |
| - command = ( |
50 |
| - ffprobe_path |
51 |
| - + " " |
52 |
| - + "-i" |
53 |
| - + " " |
54 |
| - + input_path |
55 |
| - + " " |
56 |
| - + "-show_entries stream=channels -select_streams a:0 -of compact=p=0:nk=1 -v 0" |
57 |
| - ) |
| 46 | + command = [ |
| 47 | + ffprobe_path, |
| 48 | + "-i", |
| 49 | + input_path, |
| 50 | + "-show_entries", |
| 51 | + "stream=channels", |
| 52 | + "-select_streams", |
| 53 | + "a:0", |
| 54 | + "-of", |
| 55 | + "compact=p=0:nk=1", |
| 56 | + "-v", |
| 57 | + "0", |
| 58 | + ] |
58 | 59 | output = subprocess.run(command, capture_output=True, text=True)
|
59 |
| - channels = output.stdout |
60 |
| - channels = channels.replace("\n", "") |
| 60 | + channels = output.stdout.strip() |
61 | 61 | return channels
|
62 | 62 |
|
63 | 63 |
|
64 | 64 | def checkOrCreateOutput(output_path, input_path):
|
65 | 65 | if output_path == "NA":
|
66 | 66 | output_path = input_path
|
67 |
| - else: |
68 |
| - pass |
69 |
| - |
70 |
| - outputdir = os.path.isdir(output_path) |
71 |
| - if outputdir: |
72 |
| - pass |
73 |
| - else: |
| 67 | + if not os.path.isdir(output_path): |
74 | 68 | output_path = os.path.dirname(output_path)
|
75 | 69 | return output_path
|
76 | 70 |
|
77 | 71 |
|
78 |
| -def callableSpectrogram(ffprobe_path, input_path, output_path): |
| 72 | +def callableSpectrogram(ffprobe_path, input_path, output_path, ffmpeg_path): |
79 | 73 | channels = getnumberchannels(ffprobe_path, input_path)
|
80 |
| - spectroname, ext = os.path.splitext(os.path.basename(input_path)) |
81 |
| - generate_spectrogram(input_path, channels, output_path, spectroname) |
| 74 | + spectroname, _ = os.path.splitext(os.path.basename(input_path)) |
| 75 | + generate_spectrogram(input_path, channels, output_path, spectroname, ffmpeg_path) |
82 | 76 |
|
83 | 77 |
|
84 | 78 | def main():
|
85 |
| - """Only runs if you are running this command on it's own""" |
86 |
| - input_path = os.path.abspath(args.input_path) |
| 79 | + """Only runs if you are running this command on its own""" |
| 80 | + input_path = args.input_path |
87 | 81 | ffprobe_path = args.ffprobe_path
|
88 | 82 | ffmpeg_path = args.ffmpeg_path
|
89 | 83 | sox_path = args.sox_path
|
90 | 84 | softwarecheck(sox_path, "sox")
|
91 | 85 | softwarecheck(ffprobe_path, "ffprobe")
|
92 | 86 | softwarecheck(ffmpeg_path, "ffmpeg")
|
93 |
| - channels = getnumberchannels(ffprobe_path, input_path) |
94 | 87 | output_path = args.output_path
|
95 | 88 | output_path = checkOrCreateOutput(output_path, input_path)
|
96 |
| - spectroname, ext = os.path.splitext(os.path.basename(input_path)) |
97 |
| - generate_spectrogram(input_path, channels, output_path, spectroname, ffmpeg_path) |
| 89 | + callableSpectrogram(ffprobe_path, input_path, output_path, ffmpeg_path) |
98 | 90 |
|
99 | 91 |
|
100 | 92 | if __name__ == "__main__":
|
|
0 commit comments