Skip to content

Fingerprint truncated MP4/M4A files with overrun mdat size - #4

Merged
zas merged 2 commits into
zas:mainfrom
ad-si:fix/truncated-mp4-mdat
Jul 10, 2026
Merged

zas merged 2 commits into
zas:mainfrom
ad-si:fix/truncated-mp4-mdat

Conversation

@ad-si

@ad-si ad-si commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Some MP4/M4A files declare an mdat box size that overruns the actual file length (e.g. truncated downloads). These previously failed to fingerprint. This handles the overrun by clamping to the real file size so the audio can still be decoded and fingerprinted.

Truncated .m4a files (interrupted download/copy) keep an mdat box header
advertising the original size, which now runs past EOF. Symphonia's isomp4
demuxer discards such an mdat and fails with "no atom pending read", even
though the audio present decodes fine.

Detect a final mdat whose declared end exceeds the file length and rewrite
its size field to the bytes on disk via a MediaSource wrapper, so the
available audio is fingerprinted (symphonia then stops at a clean EOF).
Also break on a demuxer DecodeError instead of failing the whole file.

@zas zas left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patch, overall it looks good, but I think we can improve it slightly, see my comments.

Comment thread src/fingerprint.rs Outdated
Comment thread src/fingerprint.rs Outdated
Address review feedback on PR zas#4:

- Move the mdat scan and PatchedSource into a new src/container_repair/
  module with a format-specific mp4 submodule, a shared Patch type, and a
  single detect_repairs(file, path, len) entry point, so future format
  repairs are just a new submodule and one match arm.
- Pass the already-open File into the scan instead of reopening the path,
  removing the redundant open and the race between the two opens. The
  scan rewinds the shared cursor when done.
- Add unit tests for the 32-bit and 64-bit mdat size repairs.
@ad-si
ad-si requested a review from zas July 10, 2026 14:49

@zas zas left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Thanks!

@zas
zas merged commit ead78ca into zas:main Jul 10, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants