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

View File

@ -11,29 +11,31 @@ import shutil
import time import time
import re import re
import logging import logging
import dvd
class Encoder(threading.Thread): class Encoder(threading.Thread):
def __init__(self, directory=None, **kwargs): def __init__(self, directory=None, **kwargs):
self.status = {} self.status = {}
self.directory = directory self.directory = directory
] return super().__init__(**kwargs) return super().__init__(**kwargs)
def run(self): def run(self):
while True: while True:
wait = True wait = True
self.status = {"type": "encoder", "state": "idle"} self.status = {"type": "encoder", "state": "idle"}
for mtype in ("audio", "video"): for fn in glob.glob(os.path.join(self.directory, "*", "sucker.json")):
for fn in glob.glob(os.path.join(self.directory, mtype, "*", "sucker.json")): fdir = os.path.dirname(fn)
fdir = os.path.dirname(fn) with open(fn) as f:
with open(fn) as f: obj = json.load(f)
obj = json.load(f) self.encode(fdir, obj)
self.encode(mtype, fdir, obj) wait = False
wait = False
if wait: if wait:
time.sleep(12) time.sleep(12)
def encode(self, mtype, fdir, obj): def encode(self, fdir, obj):
if mtype == "audio": self.status["state"] = "encoding"
self.status["title"] = obj["title"]
if obj["type"] == "audio":
self.encode_audio(fdir, obj) self.encode_audio(fdir, obj)
else: else:
self.encode_video(fdir, obj) self.encode_video(fdir, obj)
@ -43,55 +45,7 @@ class Encoder(threading.Thread):
logging.error("Not implemented") logging.error("Not implemented")
def encode_video(self, fdir, obj): def encode_video(self, fdir, obj):
self.status["state"] = "encoding" enc = dvd.Encoder(fdir, self.status)
title = os.path.basename(fdir) enc.encode(obj)
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,
)
# vi: sw=4 ts=4 et ai # 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("Error in disc handler: %s" % e)
logging.error(traceback.format_exc()) logging.error(traceback.format_exc())
self.eject() self.eject()
elif rv == CDS_DISC_OPEN: elif rv == CDS_TRAY_OPEN:
pass time.sleep(3)
else: else:
logging.info("CDROM_DRIVE_STATUS: %d (%s)" % (rv, CDS_STR[rv])) logging.info("CDROM_DRIVE_STATUS: %d (%s)" % (rv, CDS_STR[rv]))
time.sleep(3) time.sleep(3)
def eject(self): def eject(self):
self.status["state"] = "ejecting" self.status["state"] = "ejecting"
print("FAKE EJECT")
time.sleep(60*60*2)
return
for i in range(20): for i in range(20):
try: try:
@ -108,7 +105,8 @@ class Reader(threading.Thread):
pass # XXX pass # XXX
def handle_data(self): 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) src.copy(self.directory)
self.finished() 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)