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