CD maybe mostly working?

This commit is contained in:
Neale Pickett 2022-08-25 10:17:25 -06:00
parent 532fc4ec23
commit 2860a1405c
2 changed files with 73 additions and 23 deletions

View File

@ -23,7 +23,7 @@ def read(device, status):
encoding="utf-8",
capture_output=True,
)
discid = p.stdout
discid = p.stdout.strip()
status["discid"] = discid
# Look it up in cddb
@ -47,13 +47,14 @@ def read(device, status):
now = time.strftime("%Y-%m-%dT%H%M%S")
num_tracks = int(discid.split()[1])
status["title"] = "Unknown CD - %s" % now
status["tracks"] = [""] * num_tracks
status["tracks"] = ["Track %02d" % i for i in range(num_tracks)]
def rip(device, status, directory):
# cdparanoia reports completion in samples
# use discid duration to figure out total number of samples
duration = int(status["discid"].split()[-1]) * SECOND # disc duration in seconds
total_samples = duration * (75 / SECOND) * 1176 # 75 sectors per second, 1176 samples per sector
status["total_samples"] = total_samples
track_num = 1
for track_name in status["tracks"]:
@ -80,49 +81,73 @@ def rip(device, status, directory):
track_num += 1
def encode(status, directory):
# Encode the tracks
track_num = 1
durations = [int(d) for d in status["discid"].split()[2:-1]]
total_duration = sum(durations)
encoded_duration = 0
for track_name in status["tracks"]:
logging.debug("Encoding track %d (%s)" % (track_num, track_name))
duration = durations[track_num-1]
argv = [
"lame",
"--brief",
"--nohist",
"--disptime", "1",
"--preset", "standard",
"-tl", status["title"],
"--tl", status["title"],
"--tn", "%d/%d" % (track_num, len(status["tracks"])),
]
if status["artist"]:
argv.extend(["-ta", status["artist"]])
if status["genre"]:
argv.extend(["-tg", status["genre"]])
if status["year"]:
argv.extend(["-ty", status["year"]])
if status.get("artist"):
argv.extend(["--ta", status["artist"]])
if status.get("genre"):
argv.extend(["--tg", status["genre"]])
if status.get("year"):
argv.extend(["--ty", status["year"]])
if track_name:
argv.extend(["-tt", track_name])
outfn = "%d - %s.mp3" % (track_num, track_name)
argv.extend(["--tt", track_name])
outfn = "%02d - %s.mp3" % (track_num, track_name)
else:
outfn = "%d.mp3" % track_num
outfn = "%02d.mp3" % track_num
argv.append("track%02d.cdda.wav" % track_num)
argv.append(outfn)
p = subprocess.Popen(
argv,
cwd = directory,
stdin = subprocess.PIPE,
stderr = subprocess.PIPE,
encoding = "utf-8",
)
p.communicate(input=track_name)
for line in p.stderr:
line = line.strip()
if "%)" in line:
p = line.split("(")[1]
p = p.split("%")[0]
pct = int(p) / 100
status["complete"] = (encoded_duration + (duration * pct)) / total_duration
print(status["complete"])
encoded_duration += duration
track_num += 1
if __name__ == "__main__":
import pprint
import sys
import json
logging.basicConfig(level=logging.DEBUG)
status = {}
read("/dev/sr0", status)
pprint.pprint(status)
if len(sys.argv) > 1:
directory = sys.argv[1]
fn = os.path.join(directory, "status.json")
f = open(fn)
status = json.load(f)
else:
logging.basicConfig(level=logging.DEBUG)
status = {}
read("/dev/sr0", status)
pprint.pprint(status)
directory = os.path.join(".", status["title"])
os.makedirs(directory, exist_ok=True)
rip("/dev/sr0", status, directory)
pprint.pprint(status)
directory = os.path.join(".", status["title"])
os.makedirs(directory, exist_ok=True)
rip("/dev/sr0", status, directory)
pprint.pprint(status)
encode(status, directory)
pprint.pprint(status)

25
src/state.py Normal file
View File

@ -0,0 +1,25 @@
#! /usr/bin/python3
import json
class State(dict):
def __init__(self, path):
super().__init__()
self.path = path
self.read()
def read(self):
try:
f = open(self.path)
except FileNotFoundError:
return
obj = json.load(f)
f.close()
for k in obj:
self[k] = obj[k]
def write(self):
f = open(self.path, "w")
json.dump(self, f)
f.close()