maybe dvd encoding working too?

This commit is contained in:
Neale Pickett 2022-08-21 17:22:06 -06:00
parent 9f738936bb
commit 3d195bf860
4 changed files with 38 additions and 107 deletions

View File

@ -4,6 +4,7 @@ import subprocess
import time
import logging
import re
import os
SECOND = 1
MINUTE = 60 * SECOND
@ -78,7 +79,7 @@ class Copier:
self.status["title"] = title
self.status["size"] = max_sector * 2048 # DVD sector size = 2048
self.status["tracks"] = [t["ix"] for t in self.collection]
obj["tracks"] = [(t["ix"], t["length"]) for t in self.collection]
def copy(self, directory):
@ -111,32 +112,30 @@ class Copier:
lastTitleSize = titleSize
self.status["complete"] = (totalBytes + titleSize) / self.status["size"]
class Encoder:
def __init__(self, basedir, status):
self.basedir = basedir
self.status = status
def encode(self, track):
self.status["state"] = "encoding"
title = os.path.basename(fdir)
self.status["title"] = title
num_tracks = len(self.status["tracks"])
for track in self.status["tracks"]:
logging.info("encoding: %s (%s)" % (title, fdir))
def encode(self, obj):
title = obj["title"]
logging.info("encoding: %s (%s)" % (title, self.basedir))
total_length = sum(t[1] for t in obj["tracks"])
finished_length = 0
for track, length in obj["tracks"]:
outfn = "%s-%d.mkv" % (title, track)
tmppath = os.path.join(fdir, outfn)
outpath = os.path.join(self.directory, outfn)
tmppath = os.path.join(self.basedir, outfn)
outpath = os.path.join(self.basedir, "..", outfn)
p = subprocess.Popen(
[
"nice",
"HandBrakeCLI",
"--json",
"--input", "%s/VIDEO_TS" % fdir,
"--input", "%s/VIDEO_TS" % self.basedir,
"--output", tmppath,
"--title", track,
"--title", str(track),
"--native-language", "eng",
"--markers",
"--loose-anamorphic",
@ -160,7 +159,13 @@ class Encoder:
m = progressRe.search(line)
if m:
progress = float(m[1])
self.status["complete"] = progress
complete = (finished_length + progress*length) / total_length
self.status["complete"] = complete
os.rename(
src=tmppath,
dst=outpath,
)
if __name__ == "__main__":

View File

@ -11,29 +11,31 @@ import shutil
import time
import re
import logging
import dvd
class Encoder(threading.Thread):
def __init__(self, directory=None, **kwargs):
self.status = {}
self.directory = directory
] return super().__init__(**kwargs)
return super().__init__(**kwargs)
def run(self):
while True:
wait = True
self.status = {"type": "encoder", "state": "idle"}
for mtype in ("audio", "video"):
for fn in glob.glob(os.path.join(self.directory, mtype, "*", "sucker.json")):
fdir = os.path.dirname(fn)
with open(fn) as f:
obj = json.load(f)
self.encode(mtype, fdir, obj)
wait = False
for fn in glob.glob(os.path.join(self.directory, "*", "sucker.json")):
fdir = os.path.dirname(fn)
with open(fn) as f:
obj = json.load(f)
self.encode(fdir, obj)
wait = False
if wait:
time.sleep(12)
def encode(self, mtype, fdir, obj):
if mtype == "audio":
def encode(self, fdir, obj):
self.status["state"] = "encoding"
self.status["title"] = obj["title"]
if obj["type"] == "audio":
self.encode_audio(fdir, obj)
else:
self.encode_video(fdir, obj)
@ -43,55 +45,7 @@ class Encoder(threading.Thread):
logging.error("Not implemented")
def encode_video(self, fdir, obj):
self.status["state"] = "encoding"
title = os.path.basename(fdir)
self.status["title"] = title
num_tracks = len(self.status["tracks"])
for track in self.status["tracks"]:
logging.info("encoding: %s (%s)" % (title, fdir))
outfn = "%s-%d.mkv" % (title, track)
tmppath = os.path.join(fdir, outfn)
outpath = os.path.join(self.directory, outfn)
p = subprocess.Popen(
[
"nice",
"HandBrakeCLI",
"--json",
"--input", "%s/VIDEO_TS" % fdir,
"--output", tmppath,
"--title", track,
"--native-language", "eng",
"--markers",
"--loose-anamorphic",
"--all-subtitles",
"--all-audio",
"--aencoder", "copy",
"--audio-copy-mask", "aac,ac3,mp3",
"--audio-fallback", "aac",
],
encoding="utf-8",
stdout=subprocess.PIPE,
stderr=None,
)
# HandBrakeCLI spits out sort of JSON.
# But Python has no built-in way to stream JSON objects.
# Hence this kludge.
progressRe = re.compile(r'^"Progress": ([0-9.]+),')
for line in p.stdout:
line = line.strip()
m = progressRe.search(line)
if m:
progress = float(m[1])
self.status["complete"] = progress
os.rename(
src=tmppath,
dst=outpath,
)
enc = dvd.Encoder(fdir, self.status)
enc.encode(obj)
# vi: sw=4 ts=4 et ai

View File

@ -75,17 +75,14 @@ class Reader(threading.Thread):
logging.error("Error in disc handler: %s" % e)
logging.error(traceback.format_exc())
self.eject()
elif rv == CDS_DISC_OPEN:
pass
elif rv == CDS_TRAY_OPEN:
time.sleep(3)
else:
logging.info("CDROM_DRIVE_STATUS: %d (%s)" % (rv, CDS_STR[rv]))
time.sleep(3)
def eject(self):
self.status["state"] = "ejecting"
print("FAKE EJECT")
time.sleep(60*60*2)
return
for i in range(20):
try:
@ -108,7 +105,8 @@ class Reader(threading.Thread):
pass # XXX
def handle_data(self):
src = dvd.Reader(self.device, self.status)
self.status["video"] = True
src = dvd.Copier(self.device, self.status)
src.copy(self.directory)
self.finished()

View File

@ -1,26 +0,0 @@
import json
class Status:
def __init__(self, path):
self.dict = {}
self.path = path
self.update()
def __setitem__(self, key, value):
self.dict[key] = value
def __getitem__(self, key):
return self.dict[key]
def update(self):
try:
f = open(self.path, "r")
except FileNotFoundError:
return
d = json.load(f)
for k in d:
self.dict[k] = d[k]
def flush(self):
f = open(self.path, "w")
json.dump(self.dict, f)