-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaax2mp3.py
executable file
·389 lines (328 loc) · 10.4 KB
/
aax2mp3.py
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#!/usr/bin/env python3
# vim: tabstop=4:softtabstop=4:shiftwidth=4:expandtab:
# -*- coding: utf-8 -*-
import os
from subprocess import check_output
import re
import argparse
from json import loads
from json import dump as jdump
import time
from unicodedata import normalize
try:
import multiprocessing
except ImportError:
multiprocessing = None
try:
from setproctitle import setproctitle
except ImportError:
def setproctitle(x):
pass
args = None
codecs = { # codec, ext, container
"mp3": ["libmp3lame", "mp3", "mp3"],
"aac": ["copy", "m4a", "m4a"],
#'m4a': ['copy', 'm4a', 'm4a'],
#'m4b': ['copy', 'm4a', 'm4b'],
#'flac': ['flac', 'flac', 'flac'],
#'opus': ['libopus', 'ogg', 'flac'],
}
def check_missing_authcode(args):
"""ensure that an authcode is available"""
if args.auth:
return False
tmp = os.environ.get("AUTHCODE", None)
if tmp:
args.auth = tmp
return False
for f in [".authcode", "~/.authcode"]:
f = os.path.expanduser(f)
if os.path.exists(f):
with open(f) as fd:
args.auth = fd.read().strip()
return False
print('authcode not found in ".authcode", "~/.authcode", "$AUTHCODE", or the command line')
return True
def missing_required_programs():
"""ensure that various dependencies are available"""
error = False
required = ["ffmpeg", "ffprobe", "mp3splt"]
found = check_output(["which"] + required).decode("utf-8")
for p in required:
if p not in found:
error = True
print(f"missing dependency - {p}")
return error
def numfix(n):
"""convert the number of seconds into the format that mp3splt prefers"""
n = float(n)
m = int(n / 60)
s = n - (m * 60)
return f"{m}.{s:.2f}"
def get_chapters(args, md):
return [x["tags"]["title"] for x in md]
def get_splitpoints(container, md):
"""figure out where mp3splt should split the file"""
splitpoints = [float(x["start_time"]) for x in md["chapters"]]
if container == "mp3":
splitpoints.append(
md["chapters"][-1]["end_time"]
) # mp3splt needs to know the end of the split. it can't assume EOF
splitpoints = [numfix(x) for x in splitpoints]
return splitpoints
def probe_metadata(args, fn):
"""
get file metadata, eg. chapters, titles, codecs. Recent version of ffprobe
can emit json which is ever so helpful
"""
if not os.path.exists(fn):
print("Derp! Input file does not exist!")
return None
cmd = [
"ffprobe",
"-v",
"error",
"-activation_bytes",
args.auth,
"-i",
fn,
"-of",
"json",
"-show_chapters",
"-show_programs",
"-show_format",
]
buf = check_output(cmd).decode("utf-8")
buf = re.sub("\s*[(](Una|A)bridged[)]", "", buf) # I don't care about abridged or not
buf = re.sub("\s+", " ", buf) # squish all whitespace runs
ffprobe = loads(buf)
return ffprobe
def split_file(args, destdir, src, md):
"""Split the file into chapters"""
splitpoints = get_splitpoints(args.container, md)
t = md["format"]["tags"]
if args.container == "mp3":
cmd = [
"mp3splt",
"-T",
"12",
"-o",
'"Chapter @n"',
"-g",
'''"r%[@N=1,@a={},@b={},@y={},@t=Chapter @n,@g=183]"'''.format(t["artist"], t["title"], t["date"]),
"-d",
f'"{destdir}"',
f'"{src}"',
" ".join(splitpoints),
]
if args.verbose or args.test:
print(cmd)
if args.test:
return
cmd = " ".join(cmd)
rv = os.system(cmd.encode("utf-8"))
if rv == 0:
os.unlink(src)
pass
else:
raise RuntimeError(f"Don't know how to split {args.container}")
def extract_image(args, destdir, fn):
output = os.path.join(destdir, "cover.jpg")
cmd = [
"ffmpeg",
"-loglevel",
"error",
"-stats",
"-activation_bytes",
args.auth,
"-n",
"-i",
fn,
"-an",
"-codec:v",
"copy",
f"{output}",
]
if os.path.exists(output) and args.overwrite:
os.unlink(output)
if args.test or args.verbose:
print("extracting cover art")
print(" ".join(cmd))
if not args.test:
buf = check_output(cmd)
return
def sanitize(s):
"""replace any unsafe characters with underscores"""
s = normalize("NFKD", s)
s = s.encode("ascii", "ignore").decode("ascii", "ignore")
s = s.replace("'", "").replace('"', "")
# s = .encode('ascii', 'ignore')).replace("'", "").replace('"', '')
# s = normalize("NFKD", s).encode('ascii', 'ignore')).replace("'", "").replace('"', '')
s = re.sub("[^a-zA-Z0-9._/-]", "_", s)
s = re.sub("_+", "_", s)
return s
def convert_file(args, fn, md):
destdir = None
try:
destdir = os.path.join(
args.outdir, md["format"]["tags"]["artist"], md["format"]["tags"]["title"].replace("/", "-")
)
except KeyError:
print(f"Metadata Error in {fn}")
return
destdir = sanitize(destdir)
if not os.path.exists(destdir):
os.makedirs(destdir)
# XXX figure out how to hook up decrypt-only, eg:
# XXX ffmpeg -activation_bytes $AUTHCODE -i input.aax -c:a copy -vn -f mp4 output.mp4
with open(f"{destdir}/metadata.json", "w") as fd:
jdump(md, fd, sort_keys=True, indent=4, separators=(",", ": "))
if args.metadata:
return
try:
extract_image(args, destdir, fn)
except Exception:
pass
if args.coverimage:
return
if "Chapter " in str(os.listdir(destdir)):
if args.verbose:
print(f"Already processed {fn}")
return
destfn = fn.replace(".aax", f".{codecs[args.container][1]}")
output = os.path.join(destdir, destfn)
if os.path.exists(output) and args.overwrite:
print(f"removing transcoded file: {output}")
os.unlink(output)
ac = "2"
ab = md["format"]["bit_rate"]
if args.mono:
ac = "1"
ab = str(int(ab) / 2)
cmd = [
"ffmpeg",
"-loglevel",
"error",
"-stats",
"-activation_bytes",
args.auth,
"-n",
"-i",
fn,
"-vn",
"-codec:a",
codecs[args.container][0],
"-ab",
ab,
"-ac",
ac,
"-map_metadata",
"-1",
"-metadata",
'title="{}"'.format(md["format"]["tags"]["title"]),
"-metadata",
'artist="{}"'.format(md["format"]["tags"]["artist"]),
"-metadata",
'album_artist="{}"'.format(md["format"]["tags"]["album_artist"]),
"-metadata",
'album="{}"'.format(md["format"]["tags"]["album"]),
"-metadata",
'date="{}"'.format(md["format"]["tags"]["date"]),
"-metadata",
'genre="{}"'.format(md["format"]["tags"]["genre"]),
"-metadata",
'copyright="{}"'.format(md["format"]["tags"]["copyright"]),
"-metadata",
'track="1/1"',
'"{}"'.format(output),
]
cmd = " ".join(cmd)
if args.test or args.verbose:
print(cmd)
print("splitpoints:", get_splitpoints(args, md))
if args.test:
return split_file(args, destdir, output, md)
t = time.time()
os.system(cmd.encode("utf-8"))
t = time.time() - t
if args.verbose:
print(f"transcoding time: {t:0.2f}s")
if args.single == True:
return
split_file(args, destdir, output, md)
def process_wrapper(fn):
global args
setproctitle(f"transcode {fn}")
md = None
try:
md = probe_metadata(args, fn)
except Exception as e:
print(f"Caught exception {e} while probing metadata")
try:
convert_file(args, fn, md)
except Exception as e:
print(f"Caught exception {e} while probing metadata")
def main():
global args
ap = argparse.ArgumentParser()
# arbitrary parameters
ap.add_argument("-a", "--authcode", default=None, dest="auth", help="Authorization Bytes")
ap.add_argument(
"-f",
"--format",
default="mp3",
choices=codecs.keys(),
dest="container",
help="output format. Default: %(default)s",
)
ap.add_argument(
"-o", "--outputdir", default="Audiobooks", dest="outdir", help="output directory. Default: %(default)s"
)
ap.add_argument(
"-p",
"--processes",
default=1,
type=int,
dest="processes",
help="number of parallel transcoder processes to run. Default: %(default)d",
)
# binary flags
ap.add_argument(
"-c", "--clobber", default=False, dest="overwrite", action="store_true", help="overwrite existing files"
)
ap.add_argument("-d", "--decrypt", default=False, dest="decrypt", action="store_true", help="only decrypt files")
ap.add_argument(
"-i", "--coverimage", default=False, dest="coverimage", action="store_true", help="only extract cover image"
)
ap.add_argument("-m", "--mono", default=False, dest="mono", action="store_true", help="downmix to mono")
ap.add_argument(
"-s", "--single", default=False, dest="single", action="store_true", help="don't split into chapters"
)
ap.add_argument("-t", "--test", default=False, dest="test", action="store_true", help="test input file(s)")
ap.add_argument("-v", "--verbose", default=False, dest="verbose", action="store_true", help="extra verbose output")
ap.add_argument(
"-x", "--extract-metadata", default=False, dest="metadata", action="store_true", help="only extract metadata"
)
ap.add_argument(nargs="+", dest="input")
args = ap.parse_args()
something_is_wrong = False
if check_missing_authcode(args):
something_is_wrong = True
if missing_required_programs():
something_is_wrong = True
if something_is_wrong:
exit(1)
if args.mono:
args.outdir += "-mono"
if multiprocessing is None:
args.processes = 1
if args.processes < 2:
for fn in args.input:
process_wrapper(fn)
else:
proc_pool = multiprocessing.Pool(processes=args.processes, maxtasksperchild=1)
setproctitle("transcode_dispatcher")
proc_pool.map(process_wrapper, args.input, chunksize=1)
os.system("stty echo")
if __name__ == "__main__":
main()