Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Bug report: totaltrack data (corner case) is not saved in the database and is also deleted on first write from the id3 tag. *Includes working patch* #4278

@ctuller99

Description

@ctuller99

Summary:
There is a subtle but important ID3 tagging bug in Ampache when updating tags using the VaInfo/getID3 integration. If a file's TRCK tag initially contains a value like 1/15 (track 1 of 15), Ampache's current tag writing logic can cause the /15 (totaltracks) to be lost on subsequent writes.

How to reproduce:

  1. Add an audio file with an ID3v2 TRCK tag of 1/15 to your Ampache library.
  2. The tag is read correctly and both track_number and totaltracks are stored in the database.
  3. Rate the track or perform any update that causes Ampache to rewrite tags.
  4. On the next read, the TRCK tag is now just 1 -- the total track count is lost.

Root Cause:

  • When updating tags, Ampache re-reads all fields from the file using getID3, which splits TRCK into track_number and totaltracks.
  • The write logic writes only track_number back to TRCK, unless additional handling is added.
  • This causes the /totaltracks portion to be lost on the next write (and permanently if not fixed).

Impact:

  • Users lose important metadata about total track count for albums, impacting library sorting, display, and syncing.
  • Any repeated tag write (such as when rating or updating a field) can cause permanent data loss for the TRCK tag.

Working Solution (Patch):
Before writing tags, ensure both track_number and totaltracks are recombined into the correct TRCK value, like so:

if (!empty($ndata['track_number'][0])) {
    $track = $ndata['track_number'][0];
    if (!empty($ndata['totaltracks'][0])) {
        $totaltracks = $ndata['totaltracks'][0];
        $ndata['track_number'] = [$track . '/' . $totaltracks];
        $this->logger->debug('Writing TRCK as: ' . $track . '/' . $totaltracks . ' (from tag read values)', [LegacyLogger::CONTEXT_TYPE => self::class]);
    } else {
        $ndata['track_number'] = [$track];
        $this->logger->debug('Writing TRCK as: ' . $track . ' (no totaltracks present)', [LegacyLogger::CONTEXT_TYPE => self::class]);
    }
}

Recommendation:

  • Place this code immediately before $vainfo->write_id3($ndata); in your tag writing functions.
  • Remove any previous attempts to split or merge TRCK elsewhere in the code to avoid conflicts.

Note; you must also undo the commit here :d28a036 to get the data to store in the first place.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions