-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmake-flac.py
128 lines (113 loc) · 3.63 KB
/
make-flac.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
#!/usr/bin/env python
# file: make-flac.py
# vim:fileencoding=utf-8:ft=python
#
# Copyright © 2012-2018 R.F. Smith <[email protected]>.
# SPDX-License-Identifier: MIT
# Created: 2012-12-22T00:12:03+01:00
# Last modified: 2020-04-01T00:19:43+0200
"""
Encodes WAV files from cdparanoia (“trackNN.cdda.wav”) to FLAC format.
Processing is done in parallel using as many subprocesses as the machine has
cores.
Information w.r.t. artist, song titles et cetera is gathered from a text file
called “album.json”, which should have the following format;
{
"title": "title of the album",
"artist": "name of the artist",
"year": 1985,
"genre": "rock",
"tracks": [
"foo",
"bar",
"spam",
"eggs"
]
}
"""
from functools import partial
import argparse
import concurrent.futures as cf
import json
import logging
import os
import subprocess as sp
import sys
__version__ = "2020.04.01"
def main():
"""Entry point for make-flac."""
setup()
tfn = "album.json"
with open(tfn) as jf:
data = json.load(jf)
keys = data.keys()
for key in ["title", "year", "genre", "artist", "tracks"]:
if key not in keys:
logging.error(f'key "{key}" not in "{tfn}"')
sys.exit(1)
logging.info("album name: " + data["title"])
logging.info("artist: " + data["artist"])
logging.info("year: " + str(data["year"]))
logging.info("genre: " + data["genre"])
num = len(data["tracks"])
with cf.ThreadPoolExecutor(max_workers=os.cpu_count()) as tp:
for idx, rv in tp.map(partial(runflac, data=data), range(num)):
tnum = idx + 1
if rv == 0:
tt = data["tracks"][idx]
logging.info(f'finished track {tnum}, "{tt}"')
else:
logging.error(f"conversion of track {tnum} failed, return code {rv}")
def setup():
"""Process command-line arguments. Check for required programs."""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--log",
default="warning",
choices=["debug", "info", "warning", "error"],
help="logging level (defaults to 'warning')",
)
parser.add_argument("-v", "--version", action="version", version=__version__)
args = parser.parse_args(sys.argv[1:])
logging.basicConfig(
level=getattr(logging, args.log.upper(), None),
format="%(levelname)s: %(message)s",
)
logging.debug(f"command line arguments = {sys.argv}")
logging.debug(f"parsed arguments = {args}")
# Check for required programs.
try:
sp.run(["flac"], stdout=sp.DEVNULL, stderr=sp.DEVNULL)
logging.debug("found “flac”")
except FileNotFoundError:
logging.error("the program “flac” cannot be found")
sys.exit(1)
def runflac(idx, data):
"""
Use the flac(1) program to convert a music file to FLAC format.
Arguments:
idx: track index (starts from 0)
data: album data dictionary
Returns:
A tuple containing the track index and return value of flac.
"""
num = idx + 1
ifn = f"track{num:02d}.cdda.wav"
args = [
"flac",
"--best",
"--totally-silent",
"-TARTIST=" + data["artist"],
"-TALBUM=" + data["title"],
"-TTITLE=" + data["tracks"][idx],
"-TDATE=" + str(data["year"]),
"-TGENRE=" + data["genre"],
f"-TTRACKNUM={num:02d}",
"-o",
f"track{num:02d}.flac",
ifn,
]
p = sp.run(args, stdout=sp.DEVNULL, stderr=sp.DEVNULL)
return (idx, p.returncode)
if __name__ == "__main__":
main()