aadsm / jsmediatags

Media Tags Reader (ID3, MP4, FLAC)
Other
752 stars 128 forks source link

M4B Chapter/Bookmarks support #109

Open RayBB opened 5 years ago

RayBB commented 5 years ago

First, I want to say thanks so much for creating and maintaining this awesome library! It works quite well and doesn't seem like it was easy to write.

Anyway, I was wondering if it's possible to add support for M4B chapters / bookmarks? I see that there should be support for MP4 but when I try to run this with my M4B file it isn't picking up the chapters (though ffmpeg -i does).

My understanding was that MP4 and M4B bookmark formats should be very similar.

If you would like a M4B file to test with you can try this (downloaded from here)

Any help with this would be greatly appreciated! :) I'm thinking of building an open source web-based audiobook player and detecting chapters would be very helpful for this.

aadsm commented 5 years ago

Thanks for your kinds words! Let me see if I can check this during the week :).

aadsm commented 5 years ago

I looked into this and after inspecting the file I noticed the chapter information lies within the moov.trak.tref.chap atom. I was not expecting this, this library only reads the moov.udta.meta atom where all the metadata resides.

I search a bit online and found that it is the Quicktime Chapters format that is stored in a very similar fashion to subtitles: https://github.com/Zeugma440/atldotnet/wiki/Focus-on-Chapter-metadata#quicktime-qt-chapters The Nero Chapters, which is another format, are actually stored in moov.udta and includes the starting time of the chapter. The QT do not and forces you to read the samples in a non-trivial way to get to the name of each chapter.

The way MP4 support is implemented is extermly naive right now, we just go down the tree of atoms and find the first one that matches the metadata. I’m surprised it has been working since this only works if the metadata happens to be the first one in the tree as we don’t backtrack.

To implement this we need:

Borewit commented 5 years ago

I may add this to music-metadata. I hope you don't mind @aadsm.

aadsm commented 5 years ago

@Borewit that would be great please do! I’m half way through but ended up not having the time to finish it yet.

Borewit commented 5 years ago

Very nice. Traversing the atoms is pretty tricky, lots of corner cases. I will be busy to coming few weeks, effort may need to wait a bit on my end as well.

Borewit commented 4 years ago

Update #302: It's a complex operation to extract the chapters. The information appears not to be directly embedded in the chap atom. This only a tiny atom holding a pointer to another track which contains the chapter information. The actual information (the chapter titles) are encoded in the raw data stream (payload of mdat atom) divided in small chunks, just before the content chunks kicks in. Extracting that from the mdat has been a true challenge. The next challenge was to attach the corresponding offsets to the chapters to understand where they start.

Borewit commented 4 years ago

I implemented extracting of MP4 chapter in music-metadata v5.0.0. I am looking to forward for your feedback @RayBB.

aadsm commented 4 years ago

@Borewit this is great, but might be a good idea to move this discussion to the appropriate project github page?