Cumatru
Cumatru

Reputation: 725

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.

Upvotes: 1

Views: 1938

Answers (2)

scotchi
scotchi

Reputation: 2419

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

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409176

The mp3 specification doesn't really have meta-data like song name, or album art. It's part of id3, and it's normally placed at the end of the file.

Upvotes: 1

Related Questions