3
3
import subprocess
4
4
from nulrdcscripts .tools .spectrogramgeneration .params import args
5
5
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"""
41
35
if "sox" in software :
42
36
version = "--version"
43
37
else :
44
38
version = "-version"
45
-
39
+
46
40
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 ]
48
44
except :
49
- raise Exception ("Cannot locate " + softwarename + ". Check path." )
50
-
45
+ raise Exception ("Cannot locate " + softwarename + ". Check path." )
46
+
51
47
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
+ )
54
58
output = subprocess .run (command , capture_output = True , text = True )
55
59
channels = output .stdout
56
- channels = channels .replace ("\n " ,"" )
60
+ channels = channels .replace ("\n " , "" )
57
61
return channels
58
62
63
+
59
64
def checkOrCreateOutput (output_path , input_path ):
60
65
if output_path == "NA" :
61
66
output_path = input_path
62
67
else :
63
68
pass
64
-
69
+
65
70
outputdir = os .path .isdir (output_path )
66
71
if outputdir :
67
72
pass
@@ -70,20 +75,27 @@ def checkOrCreateOutput(output_path, input_path):
70
75
return output_path
71
76
72
77
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
+
73
84
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"""
75
86
input_path = os .path .abspath (args .input_path )
76
87
ffprobe_path = args .ffprobe_path
77
88
ffmpeg_path = args .ffmpeg_path
78
89
sox_path = args .sox_path
79
- softwarecheck (sox_path ,"sox" )
90
+ softwarecheck (sox_path , "sox" )
80
91
softwarecheck (ffprobe_path , "ffprobe" )
81
- softwarecheck (ffmpeg_path ,"ffmpeg" )
92
+ softwarecheck (ffmpeg_path , "ffmpeg" )
82
93
channels = getnumberchannels (ffprobe_path , input_path )
83
94
output_path = args .output_path
84
95
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 )
87
98
88
99
89
- main ()
100
+ if __name__ == "__main__" :
101
+ main ()
0 commit comments