MP3 file format analysis (1)

Source: Internet
Author: User
Tags comparison table id3 id3 tag talb

After downloading a pile of mp3 files, such as songs and music scores, it is difficult to manage and disorganized. As a result, repeated songs are everywhere, occupying a large disk, almost 80% of the songs are never heard. I always wanted to find a good tool and software to manage all the music files on the disk. Then I listened and filtered them, and gradually divided them into different categories. I deleted duplicate and dislike files, but I couldn't find the right one, therefore, you have to write a simple management tool by yourself. You only need to be able to quickly find and filter the basic information such as singers, lyrics, albums, and composing, and conveniently generate and manage m3u and other playlists. Later, I found a Zortam MP3 Media Studio on the Internet, but it is not easy to filter and query songs and automatically generate the playback list editing instructions, I also want to manage the lyrics and mp3 files in a unified manner, which is not ideal, so I decided to write it by myself.

Some basic information about mp3 is actually included in the file, including albums and singers. Therefore, we need to study the file format of mp3. The following is a document searched from Baidu encyclopedia.

Http://baike.baidu.com/view/66078.html

ID3 Introduction

The ID3 tag is an additional song message in an MP3 music file. It can be used to append the song performer, author, and other types of information to facilitate the management of many music files. The absence of the ID3 tag does not affect the playing of MP3 files, but management of music files will be quite troublesome. If you download MP3 on the Internet, most of the preset ID3 messages have been written. If you want to clear and reset it, you can use the "Alt + 3" hotkey in WinAMP to modify the ID3 tag in the MP3 file.

ID3 is usually located in several bytes at the beginning or end of an mp3 file. It carries information about the mp3 artist, title, album name, age, style, and so on, this information is called ID3 information. ID3 information is divided into two versions: v1 and v2.

The ID3 of v1 is 128 bytes at the end of the mp3 file. It starts with a TAG and follows the song information.

V2 is generally at the beginning of mp3 and can store large amounts of information such as lyrics, pictures of the album, and so on.

In addition, ID3 is ID3Tags, where "Tag" refers to "Tag" in English, so "ID3Tags" refers to "MP3 file track Tag.

In some cases, "ID3" stands for "ID3Tags", which is essentially the same.

Without ID3, people can only judge the content of a song based on the file name. Even if they hear the song, you may not be able to remember the name of the singer or album, therefore, adding built-in tags to MP3 files is a matter of course. Interestingly, the MP3 tag does not have an international ISO standard. Currently, ID3 is only a standard that is almost de facto, and no one forces the player or encoding program to support it.

 

ID3 version

ID3 V1 was invented by Eric Kemp in 1996. ID3 V1 is a set of data appended to a music file. Its length is fixed to 128 bytes.

Among them, 79 types of genre are defined, and the popular playing software Winamp extends it to 125 types, from Blues to Noise to Dance Hall, which is very rich, this list can be seen in many software that supports ID3 editing, but Chinese users do not seem to care much about this mark. Most people do not even know which category their songs belong.

The content contains a total of 128 bytes, which is not cumbersome. Because it is appended to the end of a file, the player does not have to consider its impact, even if the ID3 tag is not supported, music restoration will not be affected-some very old players may make noise or report errors when playing the video here. However, marking at the end of a file also causes a problem, that is, if we haven't read the end of the file, we can't see any information. This is certainly not a problem with the songs in the hard disk and flash memory, but if you listen directly on the network through streaming media, you will find its defects.

Careful people will notice that ID3V1.0 does not include the definition of the track number, so Michael Mutschler improved in 1997 and introduced version 1.1. By occupying the last two bytes of the remarks field, one 00 byte is used as the mark, and the other byte is changed to the serial number, so that ID3 supports the track number. One byte space allows ID3 V1.1 to support a maximum of 255 track numbers. Considering the possibility of more than 256 tracks for a single record, this improvement is quite reasonable.

ID3 format

ID3v1

ID3V1 is relatively simple. It is stored at the end of an MP3 file. Open an MP3 file in a hexadecimal editor and view the 128 bytes at the end of the MP3 file. The data structure is defined as follows:

Char Header [3];/* the label Header must be "TAG"; otherwise, no label is considered */

Char Title [30];/* Title */

Char Artist [30];/* Author */

Char Album [30];/* Special set */

Char Year [4];/* production date */

Char Comment [30];/* remarks */

Char Genre;/* type */

The information of ID3V1 is stored sequentially without any identifiers. For example, if the title information is less than 30 bytes, use '/0' to fill it up. Otherwise, an information error occurs. Genre uses the original code. The comparison table is as follows:

/* Standard genres */
0 = "Blues ";
1 = "ClassicRock ";
2 = "Country ";
3 = "Dance ";
4 = "Disco ";
5 = "Funk ";
6 = "Grunge ";
7 = "Hip-Hop ";
8 = "Jazz ";
9 = "Metal ";
10 = "NewAge ";
11 = "Oldies ";
12 = "Other ";
13 = "Pop ";
14 = "R & B ";
15 = "Rap ";
16 = "Reggae ";
17 = "Rock ";
18 = "Techno ";
19 = "Industrial ";
20 = "Alternative ";
21 = "Ska ";
22 = "DeathMetal ";
23 = "Pranks ";
24 = "Soundtrack ";
25 = "Euro-Techno ";
26 = "Ambient ";
27 = "Trip-Hop ";
28 = "Vocal ";
29 = "Jazz + Funk ";
30 = "Fusion ";
31 = "Trance ";
32 = "Classical ";
33 = "Instrumental ";
34 = "Acid ";
35 = "House ";
36 = "Game ";
37 = "SoundClip ";
38 = "Gospel ";
39 = "Noise ";
40 = "AlternRock ";
41 = "Bass ";
42 = "Soul ";
43 = "Punk ";
44 = "Space ";
45 = "Meditative ";
46 = "InstrumentalPop ";
47 = "InstrumentalRock ";
48 = "Ethnic ";
49 = "Gothic ";
50 = "Darkwave ";
51 = "Techno-Industrial ";
52 = "Electronic ";
53 = "Pop-Folk ";
54 = "Eurodance ";
55 = "Dream ";
56 = "SouthernRock ";
57 = "Comedy ";
58 = "Cult ";
59 = "Gangsta ";
60 = "Top40 ";
61 = "ChristianRap ";
62 = "Pop/Funk ";
63 = "Jungle ";
64 = "NativeAmerican ";
65 = "Cabaret ";
66 = "NewWave ";
67 = "Psychadelic ";
68 = "Rave ";
69 = "Showtunes ";
70 = "Trailer ";
71 = "Lo-Fi ";
72 = "Tribal ";
73 = "AcidPunk ";
74 = "AcidJazz ";
75 = "Polka ";
76 = "Retro ";
77 = "Musical ";
78 = "Rock & Roll ";
79 = "HardRock ";
/* Extended genres */
80 = "Folk ";
81 = "Folk-Rock ";
82 = "NationalFolk ";
83 = "Swing ";
84 = "FastFusion ";
85 = "Bebob ";
86 = "Latin ";
87 = "Revival ";
88 = "Celtic ";
89 = "Bluegrass ";
90 = "Avantgarde ";
91 = "GothicRock ";
92 = "ProgessiveRock ";
93 = "PsychedelicRock ";
94 = "SymphonicRock ";
95 = "SlowRock ";
96 = "BigBand ";
97 = "Chorus ";
98 = "EasyListening ";
99 = "Acoustic ";
100 = "Humour ";
101 = "Speech ";
102 = "Chanson ";
103 = "Opera ";
104 = "ChamberMusic ";
105 = "Sonata ";
106 = "Symphony ";
107 = "BootyBass ";
108 = "Primus ";
109 = "PornGroove ";
110 = "Satire ";
111 = "SlowJam ";
112 = "Club ";
113 = "Tango ";
114 = "Samba ";
115 = "Folklore ";
116 = "balglad ";
117 = "powerbalglad ";
118 = "RhythmicSoul ";
119 = "Freestyle ";
120 = "Duet ";
121 = "PunkRock ";
122 = "DrumSolo ";
123 = "Acapella ";
124 = "Euro-House ";
125 = "DanceHall ";
126 = "Goa ";
127 = "Drum & Bass ";
128 = "Club-House ";
129 = "Hardcore ";
130 = "Terror ";
131 = "Indie ";
132 = "BritPop ";
133 = "Negerpunk ";
134 = "PolskPunk ";
135 = "Beat ";
136 = "ChristianGangstaRap ";
137 = "HeavyMetal ";
138 = "BlackMetal ";
139 = "Crossover ";
140 = "ContemporaryChristian ";
141 = "ChristianRock ";
142 = "Merengue ";
143 = "Salsa ";
144 = "TrashMetal ";
145 = "Anime ";
146 = "JPop ";
147 = "Synthpop ";

 

 

ID3V2

Up to now, ID3V2 has a total of four versions, but the popular playback software only supports version 3rd, both ID3v2.3. Because ID3V1 is recorded at the end of the MP3 file, ID3V2 has to be recorded at the header of the MP3 file. For this reason, the operation on ID3V2 is slower than that on ID3V1. In addition, the ID3V2 structure is much more complex than the ID3V1 structure, but it is more comprehensive and scalable than the former.

The following describes ID3V2.3.

Each ID3V2.3 tag consists of a tag header and several tag frames or an Extended Tag header. Information about tracks, such as titles and authors, are stored in different tag frames. It is not necessary to expand the tag header and tag frame, but each tag must have at least one tag frame. The tag header and the tag frame are sequentially stored in the MP3 file header.

I. Label Header

Record the header of the 10-byte ID3V2.3 In the first sequence of the file. The data structure is as follows:

Char Header [3];/* must be "ID3"; otherwise, the tag does not exist */

INT Ver;/* version number; record 03 for ID3V2.3 and 04 for ID3V2.4 */

INT Revision;/* minor version number; this version is recorded as 00 */

Char Flag;/* indicates the byte of the Flag. This version defines only three characters, which will be detailed later */

Char Size [4];/* tag Size, including the 10 bytes of the tag header and the Size of all tag frames */

1. Flag bytes

The flag byte is generally 0 and is defined as follows:

Abc00000

A -- indicates whether to use Unsynchronisation (this word does not know what it means and is not found in the dictionary. It is generally not set)

B -- indicates whether there is an extension header. Generally, there is no (at least the Winamp has no records), so it is not set.

C -- indicates whether it is a test label (99.99% of the labels are not used for testing, so they are generally not set)

2. Label Size

There are four bytes in total, but each byte only uses 7 bits, and the maximum bit does not use a constant of 0. The format is as follows:

0 xxxxxxx 0 xxxxxxx 0 xxxxxxx 0 xxxxxxx

To calculate the size of a tag, remove the value 0 and obtain a 28-bit binary number. The formula is as follows:

Int total_size;

Total_size = (Size [0] & 0x7F) x 0x200000

+ (Size [1] & 0x7F) * 0x4000

+ (Size [2] & 0x7F) * 0x80

+ (Size [3] & 0x7F)

  Ii. Tag Frame

Each tag frame consists of a 10-byte frame header and at least one non-fixed-length content. They are also stored in files in sequence, and are separated from the tag header and other tag frames without special characters. The content of a complete frame can only be read after the content size is obtained from the frame header. Pay attention to the size during reading. Do not read the content or header of other frames.

The frame header is defined as follows:

Char FrameID [4];/* identifies a frame with four characters, indicating its content. A common identification table will be provided later */

Char Size [4];/* Size of the frame content, excluding the frame header. It must be no less than 1 */

Char Flags [2];/* stores the flag, which is defined as only 6 characters. For more information, see */

1. Frame ID

A frame is identified by four characters, indicating the content of a frame. The common comparison is as follows:

TIT2 = title indicates the title of the song, the same below

TPE1 = author

TALB = Special set

TRCK = audio track format: N/M where N is the first N in the album, M is the first M in the album, and N and M are ASCII numbers.

TYER = the age is an ASCII number.

TCON = the type is directly represented by a string

COMM = remarks: "eng/0 remarks", where eng indicates the natural language used for the remarks

2. Size

This algorithm does not have the label header, so it is troublesome to use all 8 bits per byte. The format is as follows:

Xxxxxxxx

The algorithm is as follows:

Int FSize;

FSize = Size [0] x 0x1000000

+ Size [1] x 0x10000

+ Size [2] x 0x100

+ Size [3];

3. Flag

Only 6 bits are defined, and the other 10 bits are 0, but in most cases, 16 bits are all 0. The format is as follows:

Abc00000 ijk00000

A -- tag protection flag, which is regarded as void when set

B -- indicates the file protection flag. This frame is considered invalid when it is set.

C -- read-only flag. When this frame is set, it cannot be modified (but I didn't find a software to ignore this flag)

I -- compression flag. When set, one byte stores two BCD codes to indicate numbers.

J -- encryption mark (I have never seen any MP3 file tag encrypted)

K -- group flag. It indicates that this frame and other frames are a group.

It is worth mentioning that when winamp saves and reads the frame content, it will add '/0' in front of the content and calculate the byte in the frame content size.

Appendix: Description of the frame ID

4. Declared ID3v2 frames

The following frames are declared in this draft.

AENC Audio encryption

APIC Attached picture

COMM Comments

COMR extends cial frame

ENCR Encryption method registration

EQUA Equalization

ETCO Event timing codes

GEOB General encapsulated object

GRID Group identification registration

IPLS Involved people list

LINK Linked information

MCDI Music CD identifier

Mllt mpeg location lookup table

OWNE Ownership frame

PRIV Private frame

PCNT Play counter

POPM Popularimeter

POSS Position synchronisation frame

RBUF Recommended buffer size

RVAD Relative volume adjustment

RVRB Reverb

SYLT Synchronized lyric/text

SYTC Synchronized tempo codes

TALB Album/Movie/Show title

Tbpm bpm (beats per minute)

TCOM Composer

TCON Content type

TCOP Copyright message

TDAT Date

TDLY Playlist delay

TENC Encoded

TEXT Lyricist/Text writer

TFLT File type

TIME Time

TIT1 Content group description

TIT2 Title/songname/content description

TIT3 Subtitle/Description refinement

TKEY Initial key

TLAN Language (s)

TLEN Length

TMED Media type

TOAL Original album/movie/show title

TOFN Original filename

TOLY Original lyricist (s)/text writer (s)

TOPE Original artist (s)/Timer mer (s)

TORY Original release year

TOWN File owner/licensee

TPE1 Lead sort mer (s)/Soloist (s)

TPE2 Band/orchestra/accompaniment

TPE3 Conductor/sort mer refinement

TPE4 Interpreted, remixed, or otherwise modified

TPOS Part of a set

TPUB Publisher

TRCK Track number/Position in set

TRDA Recording dates

TRSN Internet radio station name

TRSO Internet radio station owner

TSIZ Size

Tsrc isrc (international standard recording code)

TSSE Software/Hardware and settings used for encoding

TYER Year

TXXX User defined text information frame

UFID Unique file identifier

USER Terms of use

USLT Unsychronized lyric/text trancoder

WCOM Commercial information

WCOP Copyright/Legal information

WOAF Official audio file webpage

WOAR Official artist/Timer mer webpage

WOAS Official audio source webpage

WORS Official internet radio station homepage

WPAY Payment

WPUB Publishers official webpage

WXXX User defined URL link frame

[Edit this section]

ID3 read

The id3 information of mp3 is stored at 128 bytes at the end of the mp3 file. The structure is as follows:

Type ID3struct = packed record

ID: array [0 .. 2] of char

Title: array [0 .. 29] of char;

Artist: array [0 .. 29] of char;

Album: array [0 .. 29} of char;

Year: array [0 .. 3] of char;

Comment: array [0 .. 29] of char;

Genre: byte;

You can read the ID3 information by reading the data at the corresponding position with a pointer:

Program showID3;

{$ Apptype console}

Var

Tempfile: file of byte; // tempfile pointer, binary file

I: integer;

X: byte;

Begin

Assign(tempfile,'1.mp3 ');

Reset (tempfile); // File Association, reset

Seek (tempfile, (filesize (tempfile)-128); // The Pointer Points to 128 bytes from the end of the file

Writeln ('Id :');

For I: = 0 to 2 do

Begin

Read (tempfile, x );

Write (chr (x ));

End;

Writeln;

Writeln ('title :');

For I: = 0 to 29 do

Begin

Read (tempfile, x );

Write (chr (x ));

End;

Writeln;

Writeln ('artist :');

For I: = 0 to 29 do

Begin

Read (tempfile, x );

Write (chr (x ));

End;

Writeln;

Writeln ('album :');

For I: = 0 to 29 do

Begin

Read (tempfile, x );

Write (chr (x ));

End;

Writeln;

Writeln ('year :');

For I: = 0 to 3 do

Begin

Read (tempfile, x );

Write (chr (x ));

End;

Writeln;

Writeln ('comment :');

For I: = 0 to 29 do

Begin

Read (tempfile, x );

Write (chr (x ));

End;

Writeln; // read each byte in sequence using the cyclic method

Close (tempfile );

End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.