from datetime import datetime from dvrip import DVRIPCam from nvr_types import File as NvrFile from nvr_types import list_local_files from nvr_types import PRIMARY_STREAM, SECONDARY_STREAM from nvr_types import H264 START = "2024-08-04 6:22:34" END = "2024-08-04 23:23:09" CHANNEL = 0 def date_today(begin = True): if begin: return datetime.now().strftime("%Y-%m-%d 00:00:00") else: return datetime.now().strftime("%Y-%m-%d 23:59:59") class NVR: def __init__(self, client) -> None: self.client:DVRIPCam = client def login(self): self.client.login() def logout(self): self.client.close() @property def channels(self): return self.client.get_channel_titles() def files(self, channel, start = None, end = None, ftype = H264, stype = SECONDARY_STREAM, json = False): if not start: start = date_today() if not end: end = date_today(False) print("Search files", start, end) for raw_file in list_local_files(self.client, startTime=start, endTime=end, filetype=ftype, channel=channel, streamType=stype): if json: yield NvrFile(raw_file, channel, stype).json else: yield NvrFile(raw_file, channel, stype) def stream_file(self, file: NvrFile): return file.generate_bytes(self.client) def save_file(self, file:NvrFile, savePath = "out.unknown"): downloaded_bytes = 0 with open(savePath, "wb") as f: for byte in file.generate_bytes(self.client): f.write(byte) downloaded_bytes += len(byte) print("\r", downloaded_bytes, "/", file.size) def download_test(self, filename = "testfile.unknown"): download_file = list(self.files(0))[0] downloaded_bytes = 0 #with open(filename, "wb") as f: # for byte in download_file.download_stream(self.client): # downloaded_bytes += len(byte) # f.write(byte) # print("\r", downloaded_bytes, "/", download_file.size)