Search code examples
c++mp3gstreamertaglib

How to extract album cover from a mp3 file without download the whole file


I'm using TabLib for extraction, but i need to know how many bytes should i download from the mp3 file, in order to be able to extract TagLib.

I've looked into mp3 specs, but i didn't found anything relevant.


Solution

  • In 99% of cases, if you pull down first the first 10 bytes, you'd then have the ID3v2 header, of which the last 4 bytes will be the size of the ID3v2 tag, which will contain the cover art.

    The ID3v2 size is a "sync-safe integer", but TagLib has a function to decode that to a normal integer:

    TagLib::ID3v2::SynchData::toUInt(const ByteVector &data)
    

    So, basically the algorithm would be:

    • Grab the first 10 bytes
    • Sanity check those bytes that they start with "ID3"
    • Read the last 4 bytes of those 10 and pass them through the function above to get the ID3v2 tag length
    • Grab that much additional data from the stream
    • Pass that block of data to TagLib
    • Extract the cover art