def __init__(self, filename, grey=False, reindex=False): # Be compatible with pathlib.Path filenames filename = str(filename).encode('utf-8') self.filename = filename self.grey = grey open(filename).close() self._myiter = None self.systime_offset = 0 need_index = True idx = index_file(self.filename) if os.path.exists(idx): try: index = json.load(open(idx)) except Exception: pass else: if index['version'] == INDEX_VERSION: self.frame = index['frame'] self.systime_offset = index['systime_offset'] self.mjpg_mode = index['mjpg_mode'] need_index = False if need_index or reindex: self.frame = [] m = lib.mkv_open(self.filename) frm = ffi.new('struct mkv_frame *') cluster_offsets = set() while lib.mkv_next(m, frm): if frm.key_frame: if frm.offset in cluster_offsets: print("FIXME: Multiple keyframes per cluster!") cluster_offsets.add(frm.key_frame) self.frame.append([frm.pts, frm.offset, frm.key_frame]) self.frame.sort() self.systime_offset = iter(self).estimate_systime_offset() # FIXME: This makes a second pass over the file parsing it self.mjpg_mode = (ffi.string(m.codec_id) == b'V_MS/VFW/FOURCC') lib.mkv_close(m) tmp = idx + '.tmp.%d' % os.getpid() with open(tmp, 'w') as fd: json.dump({'frame': self.frame, 'systime_offset': self.systime_offset, 'mjpg_mode': self.mjpg_mode, 'version': INDEX_VERSION}, fd) try: os.link(tmp, idx) except FileExistsError: pass os.unlink(tmp)
def __init__(self, filename, grey=False, reindex=False): if sys.version_info > (3, ): filename = bytes(filename, "utf8") self.filename = filename self.grey = grey open(filename).close() self._myiter = None self.systime_offset = 0 need_index = True idx = index_file(self.filename) if os.path.exists(idx): index = json.load(open(idx)) if index['version'] == INDEX_VERSION: self.frame = index['frame'] self.systime_offset = index['systime_offset'] self.mjpg_mode = index['mjpg_mode'] need_index = False if need_index or reindex: self.frame = [] m = lib.mkv_open(self.filename) frm = ffi.new('struct mkv_frame *') cluster_offsets = set() while lib.mkv_next(m, frm): if frm.key_frame: if frm.offset in cluster_offsets: print("FIXME: Multiple keyframes per cluster!") cluster_offsets.add(frm.key_frame) self.frame.append([frm.pts, frm.offset, frm.key_frame]) self.frame.sort() self.systime_offset = iter(self).estimate_systime_offset( ) # FIXME: This makes a second pass over the file parsing it self.mjpg_mode = (ffi.string(m.codec_id) == b'V_MS/VFW/FOURCC') lib.mkv_close(m) with open(idx, 'w') as fd: json.dump( { 'frame': self.frame, 'systime_offset': self.systime_offset, 'mjpg_mode': self.mjpg_mode, 'version': INDEX_VERSION }, fd)
def __init__(self, filename, systime_offset, grey=False): self.m = lib.mkv_open(filename) self.systime_offset = systime_offset self.frm = ffi.new('struct mkv_frame *') self.out_of_packages = False lib.mkv_next(self.m, self.frm) assert self.m.codec_private assert self.m.codec_private_len > 0 if ffi.string(self.m.codec_id) == b'V_MS/VFW/FOURCC': self.decoder = MjpgDecoder() else: self.decoder = H264Decoder() self.decoder.open(self.m) self.fcnt = 0 self.pts = ffi.new('uint64_t *') if grey: self.channels = 1 else: self.channels = 3 if not self.m: raise IOError("Failed to open: " + filename)
def serial_number(self): """ The Axis serial number or mac address of the camera that made this recording. """ self.myiter.next() return ffi.string(self.myiter.m.mac)