def load(self, filename): self.filename = filename fileobj = xbmcvfs.File(filename) try: atoms = Atoms(fileobj) except AtomError as err: reraise(error, err, sys.exc_info()[2]) try: self.info = MP4Info(atoms, fileobj) except error: raise except Exception as err: reraise(MP4StreamInfoError, err, sys.exc_info()[2]) if not MP4Tags._can_load(atoms): self.tags = None self._padding = 0 else: try: self.tags = self.MP4Tags(atoms, fileobj) except error: raise except Exception as err: reraise(MP4MetadataError, err, sys.exc_info()[2]) else: self._padding = self.tags._padding
def save(self, filename, padding=None): """Save the metadata to the given filename.""" values = [] items = sorted(self.items(), key=self._key_sort) for key, value in items: atom_name = _key2name(key)[:4] if atom_name in self.__atoms: render_func = self.__atoms[atom_name][1] else: render_func = type(self).__render_text try: values.append(render_func(self, key, value)) except (TypeError, ValueError) as s: reraise(MP4MetadataValueError, s, sys.exc_info()[2]) for key, failed in iteritems(self._failed_atoms): # don't write atoms back if we have added a new one with # the same name, this excludes freeform which can have # multiple atoms with the same key (most parsers seem to be able # to handle that) if key in self: assert _key2name(key) != b"----" continue for data in failed: values.append(Atom.render(_key2name(key), data)) data = Atom.render(b"ilst", b"".join(values)) # Find the old atoms. fileobj = xbmcvfs.File(filename) try: atoms = Atoms(fileobj) except AtomError as err: reraise(error, err, sys.exc_info()[2]) self.__save(fileobj, atoms, data, padding)
def parse(self, data): try: return data.decode("utf-16-le").strip("\x00") except UnicodeDecodeError as e: reraise(ASFError, e, sys.exc_info()[2])