Skip to content

Commit 6b48332

Browse files
authored
Merge pull request #126 from nulib/UpdateSpectrogramGeneration
Makes better spectrograms and can be used for singles without aproc o…
2 parents 8debfb0 + 047e10d commit 6b48332

File tree

1 file changed

+61
-49
lines changed
  • nulrdcscripts/tools/spectrogramgeneration

1 file changed

+61
-49
lines changed

nulrdcscripts/tools/spectrogramgeneration/main.py

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,70 @@
33
import subprocess
44
from nulrdcscripts.tools.spectrogramgeneration.params import args
55

6-
def generate_spectrogram(input_path,channels,output_path,spectroname,ffmpegpath):
7-
''' Creates a spectrogram for each audio track in the input'''
8-
#for index, item in enumerate(channels):
9-
# spectrogram_resolution="1928x1080"
10-
# output = os.path.join(output_path, spectroname + "_spectrogram0" + str(index+1)+"_s.png")
11-
# spectrogram_args=[ffmpegpath]
12-
# spectrogram_args += ["-loglevel","error","-y"]
13-
# spectrogram_args += ["-i",input_path,"-lavfi"]
14-
# item = int(item)
15-
# if item > 1:
16-
# spectrogram_args += ["[0:a:%(a)s]showspectrumpic=mode=separate:s=%(b)s" % {"a":index,"b":spectrogram_resolution}]
17-
# else:
18-
# spectrogram_args += ["[0:a:%(a)s]showspectrumpic=s=%(b)s"%{"a":index,"b":spectrogram_resolution}]
19-
# spectrogram_args += [output]
20-
# subprocess.run(spectrogram_args)
21-
# print("*** Spectrogram Generated ***")
22-
23-
sox_spectrogram_command = [
24-
args.sox_path,
25-
input_path,
26-
"-n",
27-
"spectrogram",
28-
"-Y",
29-
"1080",
30-
"-x",
31-
"1920",
32-
"-o",
33-
os.path.join(
34-
output_path, spectroname + "spectrogram_s.png"
35-
),
36-
]
37-
subprocess.run(sox_spectrogram_command)
38-
39-
def softwarecheck (software,softwarename):
40-
'''Checks that software exists'''
6+
7+
def generate_spectrogram(input_path, channels, output_path, spectroname, ffmpegpath):
8+
"""Creates a spectrogram for each audio track in the input"""
9+
for index, item in enumerate(channels):
10+
spectrogram_resolution = "1928x1080"
11+
output = os.path.join(
12+
output_path, spectroname + "_spectrogram0" + str(index + 1) + "_s.png"
13+
)
14+
spectrogram_args = [ffmpegpath]
15+
spectrogram_args += ["-loglevel", "error", "-y"]
16+
spectrogram_args += ["-i", input_path, "-lavfi"]
17+
item = int(item)
18+
if item > 1:
19+
spectrogram_args += [
20+
"[0:a:%(a)s]showspectrumpic=mode=separate:s=%(b)s"
21+
% {"a": index, "b": spectrogram_resolution}
22+
]
23+
else:
24+
spectrogram_args += [
25+
"[0:a:%(a)s]showspectrumpic=s=%(b)s"
26+
% {"a": index, "b": spectrogram_resolution}
27+
]
28+
spectrogram_args += [output]
29+
subprocess.run(spectrogram_args)
30+
print("*** Spectrogram Generated ***")
31+
32+
33+
def softwarecheck(software, softwarename):
34+
"""Checks that software exists"""
4135
if "sox" in software:
4236
version = "--version"
4337
else:
4438
version = "-version"
45-
39+
4640
try:
47-
subprocess.check_output([software,version]).decode("ascii").rstrip().splitlines()[0].split()[2]
41+
subprocess.check_output([software, version]).decode(
42+
"ascii"
43+
).rstrip().splitlines()[0].split()[2]
4844
except:
49-
raise Exception ("Cannot locate " + softwarename +". Check path.")
50-
45+
raise Exception("Cannot locate " + softwarename + ". Check path.")
46+
5147

52-
def getnumberchannels(ffprobe_path,input_path):
53-
command = ffprobe_path +" "+ "-i" + " " + input_path + " " + "-show_entries stream=channels -select_streams a:0 -of compact=p=0:nk=1 -v 0"
48+
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+
)
5458
output = subprocess.run(command, capture_output=True, text=True)
5559
channels = output.stdout
56-
channels = channels.replace("\n","")
60+
channels = channels.replace("\n", "")
5761
return channels
5862

63+
5964
def checkOrCreateOutput(output_path, input_path):
6065
if output_path == "NA":
6166
output_path = input_path
6267
else:
6368
pass
64-
69+
6570
outputdir = os.path.isdir(output_path)
6671
if outputdir:
6772
pass
@@ -70,20 +75,27 @@ def checkOrCreateOutput(output_path, input_path):
7075
return output_path
7176

7277

78+
def callableSpectrogram(ffprobe_path, input_path, output_path):
79+
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)
82+
83+
7384
def main():
74-
'''Only runs if you are running this command on it's own'''
85+
"""Only runs if you are running this command on it's own"""
7586
input_path = os.path.abspath(args.input_path)
7687
ffprobe_path = args.ffprobe_path
7788
ffmpeg_path = args.ffmpeg_path
7889
sox_path = args.sox_path
79-
softwarecheck(sox_path,"sox")
90+
softwarecheck(sox_path, "sox")
8091
softwarecheck(ffprobe_path, "ffprobe")
81-
softwarecheck(ffmpeg_path,"ffmpeg")
92+
softwarecheck(ffmpeg_path, "ffmpeg")
8293
channels = getnumberchannels(ffprobe_path, input_path)
8394
output_path = args.output_path
8495
output_path = checkOrCreateOutput(output_path, input_path)
85-
spectroname,ext = os.path.splitext(os.path.basename(input_path))
86-
generate_spectrogram(input_path,channels,output_path,spectroname,ffmpeg_path)
96+
spectroname, ext = os.path.splitext(os.path.basename(input_path))
97+
generate_spectrogram(input_path, channels, output_path, spectroname, ffmpeg_path)
8798

8899

89-
main()
100+
if __name__ == "__main__":
101+
main()

0 commit comments

Comments
 (0)