Skip to content

Commit 28d6126

Browse files
authored
Merge pull request #127 from nulib/testaprocnewspectrogram
Updated spectrogram tool
2 parents 6b48332 + f91712a commit 28d6126

File tree

4 files changed

+172
-47
lines changed

4 files changed

+172
-47
lines changed

nulrdcscripts/tools/spectrogramgeneration/main.py

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,92 @@
1-
import progressbar
21
import os
32
import subprocess
3+
import progressbar
44
from nulrdcscripts.tools.spectrogramgeneration.params import args
55

66

77
def generate_spectrogram(input_path, channels, output_path, spectroname, ffmpegpath):
88
"""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)):
1113
output = os.path.join(
12-
output_path, spectroname + "_spectrogram0" + str(index + 1) + "_s.png"
14+
output_path, f"{spectroname}_spectrogram0{index + 1}_s.png"
1315
)
1416
spectrogram_args = [ffmpegpath]
1517
spectrogram_args += ["-loglevel", "error", "-y"]
1618
spectrogram_args += ["-i", input_path, "-lavfi"]
17-
item = int(item)
18-
if item > 1:
19+
if int(channels) > 1:
1920
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}"
2222
]
2323
else:
2424
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}"
2726
]
2827
spectrogram_args += [output]
2928
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()
3132

3233

3334
def softwarecheck(software, softwarename):
3435
"""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"
4037
try:
4138
subprocess.check_output([software, version]).decode(
4239
"ascii"
4340
).rstrip().splitlines()[0].split()[2]
4441
except:
45-
raise Exception("Cannot locate " + softwarename + ". Check path.")
42+
raise Exception(f"Cannot locate {softwarename}. Check path.")
4643

4744

4845
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+
]
5859
output = subprocess.run(command, capture_output=True, text=True)
59-
channels = output.stdout
60-
channels = channels.replace("\n", "")
60+
channels = output.stdout.strip()
6161
return channels
6262

6363

6464
def checkOrCreateOutput(output_path, input_path):
6565
if output_path == "NA":
6666
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):
7468
output_path = os.path.dirname(output_path)
7569
return output_path
7670

7771

78-
def callableSpectrogram(ffprobe_path, input_path, output_path):
72+
def callableSpectrogram(ffprobe_path, input_path, output_path, ffmpeg_path):
7973
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)
8276

8377

8478
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
8781
ffprobe_path = args.ffprobe_path
8882
ffmpeg_path = args.ffmpeg_path
8983
sox_path = args.sox_path
9084
softwarecheck(sox_path, "sox")
9185
softwarecheck(ffprobe_path, "ffprobe")
9286
softwarecheck(ffmpeg_path, "ffmpeg")
93-
channels = getnumberchannels(ffprobe_path, input_path)
9487
output_path = args.output_path
9588
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)
9890

9991

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

poetry.lock

Lines changed: 50 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "nul-rdc-scripts"
3-
version = "0.4.1"
3+
version = "0.5.0"
44
description = "Scripts for NUL RDC Digitization Team"
55
authors = [
66
"Northwestern University Libraries <[email protected]>",
@@ -14,6 +14,7 @@ readme = "README.md"
1414

1515
[tool.poetry.dependencies]
1616
python = "^3.10"
17+
progressbar2 = "^4.5.0"
1718

1819
[tool.poetry.dev-dependencies]
1920

0 commit comments

Comments
 (0)