Library exception "invalid chapter count" when writing M4B file

I did some research on this issue yesterday as I was having the problem with about 20% of my 500 audiobooks.

The M4B in question creates this error in the console when it throws the 'Can't write tags' dialog box:

Library exception during writing of MP4 tag to file '/Users/xxxxxxxx/Desktop/Too Like the Lightning.m4b': invalid chapter count.

and:

Cannot write tags to file "Too Like the Lightning.m4b" in folder "Desktop" (cannotWrite(file:///Users/xxxxxxx/Desktop/Too%20Like%20the%20Lightning.m4b))

I can also confirm:

  1. The M4B shows no information other than Filename and Path in MP3Tag.
  2. The file works fine with Apple Books
  3. The file is read fine in Plex, Prologue and AudioBookshelf.
  4. I just tried it now with the Version 1.8.12 (94) update. No change.
  5. Other files work fine from the same file location.

I can PM you the file if you like.

Many thanks for the report and your offer to share the file with me. I've sent you a PM with a link to a private upload location.

Many thanks for providing the example file. I've analyzed it, and it has a chpl atom (the part that holds the chapter information) with a chapter count of 1214701609 (the correct number is 35). I've made the decision with Mp3tag to not simply ignore those issues, because writing to a file that has a potentially incorrect structure could result in even further damage to the file.

Do you know which software you used to create those files? If so, it would be helpful to test if the error is reproducible and possibly report it to the developers.

I've also tried to find a way to correct the files and came up with this ffmpeg command line that copies everything from test.m4b to test-out.m4b while also preserving metadata.

ffmpeg -i test.m4b -vcodec copy -acodec copy test-out.m4b