Skip to content

Commit

Permalink
Recurse m4a atoms to search for ilst
Browse files Browse the repository at this point in the history
  • Loading branch information
sezanzeb committed Aug 13, 2023
1 parent fd71e21 commit 348b7a0
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions mutagen/mp4/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,17 +340,27 @@ def __init__(self, *args, **kwargs):
if args or kwargs:
self.load(*args, **kwargs)

def load(self, atoms, fileobj):
try:
path = atoms.path(b"moov", b"udta", b"meta", b"ilst")
except KeyError as key:
raise MP4MetadataError(key)

free = _find_padding(path)
self._padding = free.datalength if free is not None else 0
def load(self, atoms: Atoms, fileobj):
for atom in atoms.atoms:
if atom.children is not None:
self._recurse_atom(atom, fileobj)

def _recurse_atom(self, parent: Atom, fileobj):
"""Recursively search for an ilst atom to read metadata from.
Recursing helps if the mp4/m4a container didn't store metadata correctly,
which is usually expected at moov.udta.meta.ilst"""
if parent.name != b"ilst":
for atom in parent.children:
if parent.name == b"meta" and atom.name == b"ilst":
free = _find_padding([parent, atom])
self._padding = free.datalength if free is not None else 0

if atom.children is not None and atom.name != 'ilst':
self._recurse_atom(atom, fileobj)
return

ilst = path[-1]
for atom in ilst.children:
for atom in parent.children:
ok, data = atom.read(fileobj)
if not ok:
raise MP4MetadataError("Not enough data")
Expand Down

0 comments on commit 348b7a0

Please sign in to comment.