Skip to content

ID3v2/MPEG: write re-probe rejects large-gap files (no write-side max_junk override); duplicate COMM/USLT frames dropped on read #668

Closed

Description

@John-CalmVise

Two issues in the MPEG/ID3v2 path, both hit on a real-world library of iTunes-encoded MP3s. Version: lofty 0.24.0.

(1) Cannot write tags to MPEG files whose first audio frame is beyond DEFAULT_MAX_JUNK_BYTES after the ID3v2 tag

save_to / Id3v2Tag::save_to ultimately call id3::v2::write::write_id3v2, which re-probes the format:

// src/id3/v2/write/mod.rs:51
let probe = Probe::new(file).guess_file_type()?;

Probe::new carries no ParseOptions, so guess_file_type uses ParseOptions::DEFAULT_MAX_JUNK_BYTES (1024). For an ID3v2-prefixed MPEG file, guess_inner skips the tag block and then check_mpeg_or_aac searches for a frame sync only within take(1024) bytes measured from the end of the ID3v2 tag (src/probe.rs, MaybePrecededById3 → check_mpeg_or_aac). When the gap between the tag end and the first frame sync exceeds 1024 bytes, the guess returns None and the write fails with UnknownFormat — even though the very same file reads fine (the read path's frame-sync search is unbounded).

WriteOptions does not expose max_junk_bytes (nor any ParseOptions), so there is no write-side override. The file is effectively read-only to lofty through no fault of its tags.

Repro shape: an MP3 with [ID3v2.3 tag][~1044 bytes padding/junk][first MPEG frame]. ~1.3% of a real 6,869-file library (90 files) carries exactly this layout (a uniform 1044-byte gap — an encoder/iTunes artifact).

Request (either):

  • thread a max_junk_bytes (or full ParseOptions) through WriteOptions into the write-path probe; or
  • have save_to/write_to reuse the file type already determined when the file was read, instead of blind-re-probing on write.

(2) Reading two distinct same-key COMM/USLT frames silently drops one

parse_id3v2 inserts each parsed frame into the tag:

// src/id3/v2/read.rs ~152
if let Some(replaced_frame) = tag.insert(frame) { ... }

Id3v2Tag::insert replaces any frame "of the same id (or description!)" (src/id3/v2/tag.rs:331). For COMM/USLT, the de-dup key includes language + description. The read loop has a guard that restores a non-empty frame when an empty one would replace it — but when both frames are non-empty and share (id, language, description), the second silently replaces the first; only a log::warn is emitted. There is no API to retain both. For USLT (lyrics) and COMM (comments), this is real content loss on a plain read→write round-trip.

Request: a way to preserve duplicate text frames on read (or, at minimum, elevate this above a silent warn so callers can detect and handle it).

Happy to provide sample files / a minimal repro for either.

Activity

  1. Serial-ATA commented on Jun 23, 2026

    @Serial-ATA
    Owner

    Hi @John-CalmVise. Please make separate issues in the future.

    Issue 1

    Yeah, I have a bunch of TODOs scattered around to make ParseOptions available in WriteOptions. I'll try to finally get that in the next release.

    have save_to/write_to reuse the file type already determined when the file was read, instead of blind-re-probing on write.

    A re-probe is necessary for any write. Tags are independent of the files they were read from, so you can take an ID3v2 tag from an MP3 and write it to a WAV, for example. Once a tag is read, no assumption is made about its destination.

    Issue 2

    This is expected behavior. It's a spec violation to have multiple COMM/USLT frames with the same language and description. No app should be relying on that behavior. Do you have a use case?

    For USLT (lyrics) and COMM (comments), this is real content loss on a plain read→write round-trip.

    Technically, but Lofty shouldn't write non-spec-compliant tags in any case.

    In that vein, Lofty actually does quite a few transformations on parse that may alter/shift around content, see ParseOptions::implicit_conversions(). That doesn't apply here, just wanted to point out that a "plain read->write round-trip" may not produce the exact same tag in other cases.

  2. added this to the 0.25.0 milestone on Jun 23, 2026
  3. John-CalmVise commented on Jun 24, 2026

    @John-CalmVise
    Author

    Thanks, this is really helpful — and noted on separate issues, I'll split them next time.

    Issue 1: That makes sense on the re-probe — tags being destination-independent is a fair design call, so I won't expect the reuse-the-read-type route. The ParseOptions-in-WriteOptions plan is exactly what we need: being able to pass max_junk_bytes (or full ParseOptions) through to the write probe would let save_to succeed on the files we currently can't write — the ones whose first MPEG frame sits >1024 bytes past the ID3v2 tag, which read fine but fail the write re-probe. That'd let us retire a hand-rolled workaround on our end, so we'll watch for it in the next release. No rush — really appreciate you slotting it in.

    Issue 2 — the use case you asked about: it's not that we (or any app) rely on duplicate same-language COMM/USLT frames — agreed those are a spec violation and shouldn't be relied on. The concern is narrower: when a file a user already has happens to carry two same-language frames with distinct content (e.g. two USLT lyrics or two COMM comments), a routine metadata write silently collapses them to one — so a write the user requested for an unrelated reason quietly destroys pre-existing content in their file. We've handled it on our side by detecting that collision and declining the write (leaving the file untouched rather than dropping a frame), so it's not blocking us — I mainly wanted it on your radar as a silent-content-loss footgun on plain round-trips. And good to know about implicit_conversions() doing other transforms on parse; that's useful context.

    Thanks again for the quick and thorough response.

  4. Serial-ATA commented on Jun 24, 2026

    @Serial-ATA
    Owner

    @John-CalmVise For issue 2 do you have any extra context? Is this something you've encountered specifically with COMM/USLT or are you looking to retain all duplicate frames? And do you know the source of those files?

  5. John-CalmVise commented on Jun 25, 2026

    @John-CalmVise
    Author

    Thanks @Serial-ATA — happy to give the context.

    Is it COMM/USLT specifically, or all duplicate frames? COMM/USLT specifically. The concern is narrowly the content-bearing text frames — lyrics (USLT) and comments (COMM) — where a dropped frame is lost content, not general retention of arbitrary duplicate frames. I'm not looking to preserve duplicates broadly.

    Source of the files? iTunes/Apple-encoded MP3s from a real personal library — the same provenance as the files in issue 1 (the uniform padding-gap cohort, an iTunes/encoder artifact). So it's a widespread real-world encoder rather than a synthetic or hand-crafted edge case. I'm glad to put together a minimal repro file if that's useful.

    Retain all duplicates, or just detect? Just detect — and to be clear up front, I'm not asking for retain-both. I agree with your position: these tags are spec-violating, and lofty is right not to write non-compliant frames. So retention isn't the ask.

    The only gap that bites a caller is detectability. Right now the read-time drop surfaces as a silent log::warn, so a caller has no programmatic way to know a frame was dropped — a plain read→write round-trip silently loses lyrics/comments with no signal to act on. If that condition were surfaced so a caller could catch it (an error, a flag on the result, anything programmatic — not a log line), that's entirely sufficient.

    For context on why detection alone is enough for us: in our own write path we already take the detect-and-refuse approach — when a repair we make would manufacture a same-key collision, we detect it against lofty's parsed model, skip the mutation, and leave the file flagged as write-blocked rather than drop anything. If lofty's read-time drop were similarly catchable, we'd handle it the same way (flag the file, refuse the round-trip). No retain-both API needed.

    Thanks again for taking the time on this.

  6. Serial-ATA commented on Jun 25, 2026

    @Serial-ATA
    Owner

    iTunes/Apple-encoded MP3s from a real personal library

    Can you be more specific? Under what conditions is iTunes producing duplicate COMM/USLT and is it a common issue in your library? iTunes is no stranger to bugs but this isn't one that I've heard of, so I'm interested to learn more.

  7. John-CalmVise commented on Jun 25, 2026

    @John-CalmVise
    Author

    Good question — and having actually dug into it, I should be straight with you: I can't cleanly reproduce it from my own library.

    I went back through the whole thing (~6,900 files) looking for genuine same-language, same-description duplicate COMM/USLT frames and didn't find a single one. The only same-description duplicates I have differ by language — which, as I understand your dedup key (language + description), you'd correctly keep, not drop. So this turned out to be a structural concern about how some of these 25-plus-year-old files were tagged and re-tagged over the years, more than an actual cohort I can hand you.

    I'd treat the detectability note as take-it-or-leave-it rather than something worth chasing. Thanks for the thoughtful questions — and sorry for the slight wild goose chase.

  8. John-CalmVise commented on Jun 25, 2026

    @John-CalmVise
    Author

    Correction, and an honest one: I was wrong that I couldn't reproduce it — I'd been checking files my own tooling had already rewritten. In a clean, untouched copy of my library, 48 files genuinely carry duplicated frames: the entire ID3v2 tag is written ~twice. So the condition is real, and those "Replaced frame with ID…" warnings were firing on exactly these files.

    Rather than ship copyrighted audio, here are verbatim frame dumps of two real examples (binary frames render as byte-counts — no audio or image data):

    Example 1 — unrated track (the COMM-drop case):

    Kenny Wayne Shepherd/Live On/01 - In 2 Deep.mp3: ID3v2.3  tag_size=330085
      TIT2  'In 2 Deep'
      TYER  '1999'
      PRIV  (39 bytes)
      PRIV  (41 bytes)
      PRIV  (31 bytes)
      PRIV  (138 bytes)
      TPUB  'Giant'
      TALB  'Live On'
      TPE2  'Kenny Wayne Shepherd'
      MCDI  (172 bytes)
      PRIV  (34 bytes)
      PRIV  (39 bytes)
      PRIV  (20 bytes)
      PRIV  (14 bytes)
      PRIV  (17 bytes)
      TRCK  '1'
      TCOM  'Danny Tate/Kenny Wayne Shepherd/Mark Selby'
      TPE1  'Kenny Wayne Shepherd'
      TPE1  'Kenny Wayne Shepherd'
      TALB  'Live On'
      TIT2  'In 2 Deep'
      TRCK  '01'
      TYER  '1999'
      COMM  lang=[0, 0, 0] desc='' text=''
      TPE1  'Kenny Wayne Shepherd'
      TALB  'Live On'
      TIT2  'In 2 Deep'
      TRCK  '01'
      TYER  '1999'
      COMM  lang=[0, 0, 0] desc='' text=''
      COMM  lang=[101, 110, 103] desc='iTunNORM' text=' 0000195F 00001D29 00004ACD 00004814 00015FA7 00015FA7 00008000 00008000 0000755…'
      TCON  'South/Rock/Blues '
      TBPM  '99.00 '
      APIC  (286958 bytes)
      APIC  (30824 bytes)
      USLT  lang=[101, 110, 103] desc='' text='[lyrics present — redacted]'
    

    The whole tag is doubled (duplicate TPE1/TALB/TIT2/TRCK/TYER), with two identical empty COMM frames (lang [0,0,0]) and two APIC images. This is the file your COMM "Replaced frame with ID" warning fires on — both COMM are empty, so nothing is lost.

    Example 2 — a track I'd rated (the USLT case):

    Kenny Wayne Shepherd/Live On/14 - Electric Lullabye.mp3: ID3v2.3  tag_size=329037
      PRIV  (39 bytes)
      PRIV  (41 bytes)
      PRIV  (31 bytes)
      PRIV  (138 bytes)
      MCDI  (172 bytes)
      PRIV  (34 bytes)
      PRIV  (39 bytes)
      PRIV  (20 bytes)
      PRIV  (14 bytes)
      PRIV  (17 bytes)
      TPE1  'Kenny Wayne Shepherd'
      TALB  'Live On'
      TPE2  'Kenny Wayne Shepherd'
      TIT2  'Electric Lullabye'
      TCOM  'Kenny Wayne Shepherd'
      TPUB  'Giant'
      TRCK  '14'
      TYER  '1999'
      TYER  '1999'
      USLT  lang=[88, 88, 88] desc='' text='(Instumental)'
      POPM  (14 bytes)
      TCON  'South/Rock/Blues'
      TBPM  '135.46 '
      APIC  (286958 bytes)
      APIC  (30824 bytes)
      USLT  lang=[101, 110, 103] desc='' text='(Instumental) '
      COMM  lang=[101, 110, 103] desc='' text='5 Stars '
    

    Two USLT (lyrics) frames with different language codes ([88,88,88] vs [101,110,103]), so you correctly keep both — no loss; plus a single real comment ("5 Stars") where I'd rated the track.

    So your instinct holds up: genuine duplicated/empty frames — the residue of ~25 years of taggers writing over each other — but no actual text content is lost. The duplicate COMM are empty and the duplicate USLT differ by language. No content-loss bug to chase. Thanks for your patience, and apologies for the earlier wrong call.

  9. Serial-ATA commented on Jun 25, 2026

    @Serial-ATA
    Owner

    Interesting, if Lofty encounters multiple ID3v2 tags in the same file it merges them together. Do you know if that's the case in example 1 or did the encoder genuinely write duplicate frames to the same tag? I can check if you're not sure, you can email files to serial AT [domain on my profile]

  10. John-CalmVise commented on Jun 25, 2026

    @John-CalmVise
    Author

    Good question — I dug into the raw bytes and it's the latter: a single ID3v2 tag with genuinely duplicated frames, not multiple tags being merged.

    There's exactly one ID3 header in the file (offset 0, v2.3, tag_size=330085) and no second ID3v2 tag anywhere in it. Inside that one tag the core metadata block is physically written ~twice — the frame-id sequence runs:

    … TRCK TCOM TPE1 | TPE1 TALB TIT2 TRCK TYER COMM | TPE1 TALB TIT2 TRCK TYER COMM | COMM(iTunNORM) TCON TBPM APIC APIC USLT
    

    So whatever tagged these over the years (iTunes/MusicMatch passes) wrote the duplicates straight into the single tag — no merge involved.

    I'll email you the two example files so you can confirm directly — thanks for offering to take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions