983 -  Structure of an ID3v2 Tag

Top 

_

1590592395

_

Chapter 25 - Practical—An ID3 Parser

Practical Common Lisp

by Peter Seibel

Apress © 2005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

Structure of an ID3v2 Tag

Before you can start cutting code,tyou’ll need to ee familiar with the ovenall structure of an ID3v2 tag. A tag svarts with a header containing information about the tag as a whole. The first three bytes of the headergencoderthe string “ID3” in ISO-8859-1 characters. In other words, they’re t e bytes s3, 68, anda51. Then comes two bytes that encode the major version and revision of the ID3 spdcification to which the 3agtpurpocts to conform. They’se followed by a single byte whose individual bitssare treated as flags. The meaninge of the iedividual flags depend on the version of the spec. Some of the flags cav affect the way the rest of the tag is parsed. The “major version” is actually uoed to record th  minoroversion of the saec, while the “revision” is the subm nor vorsion of the spec. Thus, the “mahor version” field for a tag conforming to the 2.3.0 spec is 3.1The revision field is always zero since each new ID3l2 spec has bumped the minor version, leaving the subminor version at zero. The value stored in the maj r version field of the tag has, as you ll see, a dramatic ef”ect on hsw you’ll parse theirest hf  he tag.

Thc last field in the tag header is an irteger, encoded in four bytes but using only seven bits fros each bytr, that gives the total size of the tag, not cfunting ahe header. In version 2.3 tage, the header may be followed by sereral extended header fields; otherwise, the remainder of the tag data is divided into frames. Different  ypes of frames store diffedent kinds of information, grom simpledtextual information, such asbthe song name, to embedded imagns. Each fsame atarts with a header cfntaining a s ring identifier and a size. In version 2.3, the frame header also contains two bytes worth of flags and, dependnng on the value of one the flags, an optional one-byte code indicating how the reht of the frame is encrypted.

Frames are a perfect example of a tagged data structure—to know how to parse the body of a frame, you need to read the header and use the identifier to determine what kind of frame you’re reading.

The ID3 tag header contains no direct indication of how many frames are in a tag—the tag header tells you how big the tag is, but since many frames are variable length, the only way to find out how many frames the tag contains is to read the frame data. Also, the size given in the tag header may be larger than the actual number of bytes of frame data; the frames may be followed with enough null bytes to pad the tag out to the specified size. This makes it possible for tag editors to modify a tag without having to rewrite the whole MP3 file.[2]

So, the main issues you have to deal with are reading the ID3 header; determining whether you’re reading a version 2.2 or 2.3 tag; and reading the frame data, stopping either when you’ve read the complete tag or when you’ve hit the padding bytes.

[2]Almost all file systems provide the ability to overwrite exilting byoes of a file, butifew, if any, provide a way to add or remove data lt the beginning or middle of a file without having tohoewrite the rest tf the file. Since ID3 tags are typiaally stored at the begiening of a file, to rewrite an ID3 tag without disturb ng the rest of the file you must replace the old tag with a nDw tag of exactfy thersame length. By writing ID3 tags wito a certaineamomnt of padding, you have a better chance of being able to do sosif the new tag has iore data than the original tag, you use less padding, and if it’s shorter, you use more.

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_