Fixed incorrect download - the ID3v1 tag class incorrectly contained the code for the ID3v2 tags...
Reading and Writing MP3 ID3v1 and ID3v2 Tags
Two classes to simplify MP3 Tag handling
This article describes the MP3 ID3v1 and ID3v2 tags and provides a sample application which can read and write both types from MP3 files, along with two reusable classes wrapping up the tag functionality.
About MP3 Tags
The MP3 file standard includes specifications for tags, known as ID3 tags, which allow you to store information within the MP3 file about the track's title, artist and so on. If you're running Windows XP, you will see that there is a Shell Extension to read these tags, which allows you to read the tags in the Properties dialog for the file, or to see them in Explorer.
There are two types of MP3 tags: the v1 original version, which is rather limited, and the v2 tags, which are much more sophisticated.
The ID3v1 tag is found at the end of an MP3 file and has a fixed number of fields and size. The structure of the tag is as follows:
Private Type MP3ID3V1Tag Tag As String * 3 '-- 03 = "TAG" Title As String * 30 '-- 33 Artist As String * 30 '-- 63 Album As String * 30 '-- 93 Year As String * 4 '-- 97 Comment As String * 30 '-- 127 Genre As Byte '-- 128 End Type
Note that v1.1 of the ID3v1 specification allows the last two bytes of the Comment tag to be used to store the track number. Byte 29 is always set to 0 in this case, and Byte 30 stores the track number itself.
Checking if an MP3 file contains an ID3v1 tag is a simple matter of rewinding 128 bytes from the end and checking if the bytes read "TAG". If they do, then the rest of the bytes are the tag itself. Reading and writing the tag is simple: if its already there, then you just fill in the structure and then write it over the last 128 bytes of the file. If the tag isn't there, then just append a structure to the end of the file.
For information, a list of known Genre bytes is given at the end of this article.
The cMP3ID3v1 class provided with the download provides a wrapper for reading and writing this tag. Properties and methods are:
All fields will be truncated so they are no longer than the length allowed in the structure prior to saving.
The ID3v2 tag is more flexible and hence more difficult to work with. An ID3v2 tag has a signature code of "ID3x" where x is the sub-version number of the tag. Typically ID3v2 tags are found at the beginning of an MP3 file but this is not an absolute restriction. What happens next depends on the subversion of the ID3v2 tag. As far as I can tell, there have been three versions so far: 2,3 and 4.
Following the ID3v2 signature there are two reserved bytes and then 4 bytes containing the size of the ID3v2 tag. Note that all sizes appear in the opposite byte-order to Win32, so to read the actual length you need to swap the byte order. Once the size has been determined then anywhere after the header until the end of the tag there can be an ID3v2 tag Frame. The structure of a Frame header depends on the sub-version of ID3v2, but always encodes the name of the frame followed by its length in bytes and optionally some padding:
Typical frame names used in the more modern ID3v2 tags are as follows (with the subversion 2 equivalent in brackets afterwards where I've found details):
It is possible to have more than one of each tag. For example, the COMM frame is used in MusicMatch to allow you tag Preferences, Notes, Bios, Moods, Tempos and other such trivial information. A different COMM frame is written out for each of these with the data within the COMM frames divided into a header (such as MusicMatch_Bio) followed by a null character and then the data.
Clearly it is more difficult to read and write this member. If the file doesn't contain an ID3v2 tag, or the ID3v2 is too short for the new data, then the MP3 data has to be shifted up until it can be read in the result. I also noted that when writing some text frames, an additional null character is needed prior to the text otherwise the data isn't read correctly. The frames affected are Lyrics (USLT), Comments (COMM) and LinkTo (WXXX).
The cMP3ID3v2 class provided with the download wraps up reading and writing this tag. There are two ways of using the class. For access to any of the standard frames given above, there are corresponding Property Gets/Lets. This does not allow you to create multiple COMM frames, though, so you can also work directly with the collection of frames.
File Properties and Methods
Standard Tag Properties and Methods
Setting any of these properties will mean there will be only one occurrence of the relevant frame. For example, if you set the Comments property on an MP3 file that had multiple COMM frames, the old COMM frames are cleared and replaced with the new comment.
Frame Collection Properties and Methods