forked from ghedo/loudgain
-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
rgbpm2
executable file
·204 lines (177 loc) · 7.53 KB
/
rgbpm2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2019 Matthias C. Hormann
from __future__ import print_function
import os
import errno
import sys
import signal
import argparse
import textwrap
import fnmatch
import subprocess
import multiprocessing
def init_worker():
"""Initialize workers so they can’t get a KeyboardInterrupt
"""
signal.signal(signal.SIGINT, signal.SIG_IGN)
def loudgain(args):
"""loudgain args
args[0] = folder
args[1] = file extension
args[2:n] = loudgain options
args[n+1:] = filenames
"""
print("Working on {} ...".format(os.path.abspath(os.path.join(args[0],
'*' + args[1]))))
sys.stdout.flush()
try:
retcode = subprocess.check_call(['loudgain', '-q'] + args[2:],
stdout=open(os.devnull, 'wb'))
except subprocess.CalledProcessError as e:
print("loudgain returned status {}, command was:\n{}".format(
e.returncode, subprocess.list2cmdline(e.cmd)), file=sys.stderr)
retcode = e.returncode
except OSError as e:
if e.errno == errno.ENOENT:
print("loudgain not found, but it is required", file=sys.stderr)
else:
print("Execution failed:", e, file=sys.stderr)
retcode = e.errno
return retcode
def main():
"""Main program
Usage: rgbpm2 [-h] [-v] folder [folder ...]
"""
# dict: file extension, loudgain cmdline args (use lowercase extensions)
# see `loudgain -h` for an explanation of options
# '.mp4' deliberately left out: these are doable but usually videos
extensions = {
'.flac': ['-a', '-k', '-s', 'e'],
'.ogg': ['-a', '-k', '-s', 'e'],
'.oga': ['-a', '-k', '-s', 'e'],
'.spx': ['-a', '-k', '-s', 'e'],
'.opus': ['-a', '-k', '-s', 'e'],
'.mp2': ['-I', '3', '-S', '-L', '-a', '-k', '-s', 'e'],
'.mp3': ['-I', '3', '-S', '-L', '-a', '-k', '-s', 'e'],
'.m4a': ['-L', '-a', '-k', '-s', 'e'],
'.wma': ['-L', '-a', '-k', '-s', 'e'],
'.asf': ['-L', '-a', '-k', '-s', 'e'],
'.wav': ['-I', '3', '-L', '-a', '-k', '-s', 'e'],
'.aif': ['-I', '3', '-L', '-a', '-k', '-s', 'e'],
'.aiff': ['-I', '3', '-L', '-a', '-k', '-s', 'e'],
'.wv': ['-S', '-a', '-k', '-s', 'e'],
'.ape': ['-S', '-a', '-k', '-s', 'e']
}
# exclude all folders ending in '[compilations]'
# special characters masked in [] (glob/fnmatch syntax)
excludes = ['*[[]compilations[]]']
parser = argparse.ArgumentParser(
description='ReplayGain album folders recursively, using loudgain.\n' +
'Files of the same type in the same folder are '
'considered an album.\n \n' +
textwrap.fill("Supported audio file types: " +
", ".join(sorted(extensions)), 75),
epilog='Please report any issues to '
'https://github.com/Moonbase59/loudgain/issues.',
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-v', '--version', action='version',
version='%(prog)s ' + __version__)
parser.add_argument('-n', '--numproc', type=int, choices=range(1, 33),
metavar='1..32', default=multiprocessing.cpu_count(),
help='set max. number of parallel processes (default: %(default)d)')
parser.add_argument('-f', '--follow-links', action='store_true',
help='follow links (default: %(default)s); use with care!')
parser.add_argument('folder', nargs='+',
help='Path of a folder of audio files.')
args = parser.parse_args()
# set number of parallel processes to use
numproc = min(args.numproc, multiprocessing.cpu_count())
# First, check if loudgain exists and show some info
try:
output = subprocess.check_output(["loudgain", "--version"],
stderr=subprocess.STDOUT)
print("Using loudgain v{}, max. {} parallel processes.".format(
output.split()[1], numproc))
sys.stdout.flush()
except OSError as e:
if e.errno == errno.ENOENT:
# handle file not found error.
print("loudgain executable not found; this is required",
file=sys.stderr)
exit(1)
else:
# Something else went wrong while trying to run `loudgain`
raise
# Traverse folders, set up list of files.
# dict: {folder: {filetype: [files]}}
cluster = dict()
stat_excluded_folders = 0
for folder in args.folder:
for root, dirs, files in os.walk(os.path.abspath(os.path.join(
os.getcwd(), folder)), followlinks=args.follow_links, topdown=True):
# remove excluded dirs "in-place", only if topdown=True (default)
for exclude in excludes:
for dir in dirs:
if fnmatch.fnmatch(dir, exclude):
print("Excluding {}".format(os.path.join(root, dir)))
sys.stdout.flush()
dirs.remove(dir)
stat_excluded_folders += 1
# add files with known extensions to cluster
for file in files:
fileName, fileExt = os.path.splitext(file)
if fileExt.lower() in extensions:
cluster.setdefault(root, {}).setdefault(fileExt.lower(),
[]).append(file)
# statitics
stat_folders = len(cluster)
stat_extensions = { ext for folder in cluster for ext in cluster[folder] }
stat_files = 0
stat_files = sum(1 for folder in cluster for ext in cluster[folder] for f in cluster[folder][ext])
# set up the tasks for multiprocesssing
# a task is one loudgain invocation (one folder, same file type)
tasks = []
for folder in cluster:
for ext in cluster[folder]:
filelist = []
for file in cluster[folder][ext]:
filelist.append(os.path.join(folder, file))
tasks.append([folder] + [ext] + extensions[ext] + filelist)
stat_tasks = len(tasks)
print("Excluded {} folders.".format(stat_excluded_folders))
print("Working on {} folders containing {} files, requiring {} tasks."
.format(stat_folders, stat_files, stat_tasks))
print("Found files of type: {}".format(", ".join(sorted(stat_extensions))))
print("=== START ===")
sys.stdout.flush()
# Set up the parallel task pool
# The initializer function prevents child processes from being aborted
# by KeyboardInterrupt (Ctrl+C).
pool = multiprocessing.Pool(processes=numproc, initializer=init_worker)
# Run the jobs
# The key magic is that we must call results.get() with a timeout, because
# a Condition.wait() without a timeout swallows KeyboardInterrupts.
results = pool.map_async(loudgain, tasks)
while not results.ready():
try:
results.wait(timeout=1)
except KeyboardInterrupt:
pool.terminate()
pool.join()
print("KeyboardInterrupt, all processes aborted!", file=sys.stderr)
exit(1)
print("=== END ===")
print("Excluded {} folders.".format(stat_excluded_folders))
print("Worked on {} folders containing {} files, requiring {} tasks."
.format(stat_folders, stat_files, stat_tasks))
print("Found files of type: {}".format(", ".join(sorted(stat_extensions))))
if not all(ret == 0 for ret in results.get()):
print("Errors occurred!", file=sys.stderr)
sys.stdout.flush()
__version_info__ = ('0', '15')
__version__ = '.'.join(__version_info__)
APPNAME = "rgbpm2 " + __version__
LICENSE = "MIT"
if __name__ == '__main__':
main()