diff --git a/backend/config_parser.py b/backend/config_parser.py index 75dcbad..8619722 100644 --- a/backend/config_parser.py +++ b/backend/config_parser.py @@ -1,3 +1,4 @@ +import traceback from genericpath import exists import os, sys from asyncio_dvrip import DVRIPCam @@ -257,12 +258,14 @@ class TranscodeTools: try: await self.processing(status, file, nvr, reCreate) except Exception as e: + traceback.print_exc() self.statuses[status.b64].error = str(e) async def processing(self, status: TranscodeStatus, file:File, nvr: NVR, reCreate:bool = False): - raw_file = os.path.join(self.transcode_directory, status.uuid + ".h264") + raw_file = file.localPath(self.transcode_directory) + self.logger.info(f"save path: {raw_file}") - mp4_file = os.path.join(self.transcode_directory, status.uuid + ".h264.avi.mp4") + mp4_file = os.path.join(f"{raw_file}.avi.mp4") if os.path.exists(mp4_file) and os.path.getsize(mp4_file) != 0: nvr.logout() self.statuses[status.b64].outFile = mp4_file diff --git a/backend/nvr_types.py b/backend/nvr_types.py index d9b4517..094feb7 100644 --- a/backend/nvr_types.py +++ b/backend/nvr_types.py @@ -20,8 +20,8 @@ H265X = "h265x" class File: def __init__(self, data, channel = 0, stream = 0) -> None: - self.begin = datetime.strptime(data.get("BeginTime", data.get("begin")), NVR_DATETIME_FORMAT) - self.end = datetime.strptime(data.get("EndTime", data.get("end")), NVR_DATETIME_FORMAT) + self.begin:datetime = datetime.strptime(data.get("BeginTime", data.get("begin")), NVR_DATETIME_FORMAT) + self.end:datetime = datetime.strptime(data.get("EndTime", data.get("end")), NVR_DATETIME_FORMAT) self.DiskNo = data.get("DiskNo") self.SerialNo = data.get("SerialNo") self.size = int(data.get("FileLength"), 0) * 1024 if "FileLength" in data else data.get("size", 0) @@ -47,16 +47,39 @@ class File: @property def json(self): b64 = self.to_b64.replace("==", "") - return {"filename": self.filename_cleared, "type": self.type, "size": self.size, "b64": b64, "converted":File.converted(b64)} + return {"filename": self.filename_cleared, "type": self.type, "size": self.size, "b64": b64, "converted":self.converted} @staticmethod def from_b64(b64): data = json.loads(base64.b64decode(b64).decode('utf-8')) return File(data, data.get("channel"), data.get("stream")) - @staticmethod - def converted(b64): - return os.path.exists(os.path.join(os.path.join(app_dir(), "transcode"), str(uuid_from_string(b64)) + ".h264.avi.mp4")) + @property + def converted(self): + return os.path.exists(self.localPath(os.path.join(os.path.join(app_dir(), "transcode"))) + ".avi.mp4") + + def genPath(self, root, subPath, ext): + if not os.path.exists(os.path.join(root, subPath)): + os.mkdir(os.path.join(root, subPath)) + + if not os.path.exists(os.path.join(root, subPath, str(self.begin.year))): + os.mkdir(os.path.join(root, subPath, str(self.begin.year))) + + if not os.path.exists(os.path.join(root, subPath, str(self.begin.year), str(self.begin.month))): + os.mkdir(os.path.join(root, subPath, str(self.begin.year), str(self.begin.month))) + + if not os.path.exists(os.path.join(root, subPath, str(self.begin.year), str(self.begin.month), str(self.begin.day))): + os.mkdir(os.path.join(root, subPath, str(self.begin.year), str(self.begin.month), str(self.begin.day))) + + if not os.path.exists(os.path.join(root, subPath, str(self.begin.year), str(self.begin.month), str(self.begin.day), str(self.begin.hour))): + os.mkdir(os.path.join(root, subPath, str(self.begin.year), str(self.begin.month), str(self.begin.day), str(self.begin.hour))) + + return os.path.join(root, subPath, str(self.begin.year), str(self.begin.month), str(self.begin.day), str(self.begin.hour), f"{self.filename_cleared.replace(' ', '_').replace('(', '_').replace(')', '_').replace('-', '').replace(':', '')}.{ext}") + def localPath(self, root, subPath = "video"): + return self.genPath(root, subPath, self.type) + + def previewPath(self, root, subPath = "preview"): + return self.genPath(root, subPath, "jpg") async def generate_first_bytes(self, client:DVRIPCam, version = 0): client.logger.debug("init request") diff --git a/backend/server.py b/backend/server.py index 6e6f7c0..b6afe2e 100644 --- a/backend/server.py +++ b/backend/server.py @@ -94,7 +94,6 @@ class Server: return "" nvr:NVR = self.config.getRecorder(recorder_index).nvr await nvr.login() - nvr.client.debug() file: File = File.from_b64(b64 + "==") async def after(): @@ -126,7 +125,6 @@ class Server: nvr:NVR = self.config.getRecorder(recorder_index).nvr await nvr.login() - nvr.client.debug() file: File = File.from_b64(b64 + "==") self.config.transcode_tools.statuses[b64] = TranscodeStatus(b64)