毕业设计(音乐播放器)之二(MP3文件标签解析)

来源:互联网 发布:免费视频剪辑软件 编辑:程序博客网 时间:2024/06/06 07:34

目前MP3文件比较通用的标签有ID3v1,ID3V2,APEv2.


一、ID3v1位于Mp3文件的最后128个字节,其中包括:

第一部分为固定的$54 14 47,表示'TAG'这三个字符

然后30个字节的空间用来表示歌曲名,30个字节表示艺术家名,30个字节表示专辑名,4个字节表示发行年份,30个字节表示注释和备注信息,1个字节表示音乐流派


二、ID3V2就复杂一点了


ID3V2位于文件开头或者APE标签之后,就我见过的大部分都位于文件开头。

ID3V2包括一个TAG Head和很多Frame,每个Frame又由Frame Head和Frame Body构成.

1.先来看TAG Head:

ID3v2/file identifier   "ID3" ID3v2 version           $03 00ID3v2 flags             %abc00000ID3v2 size              4 * %0xxxxxxx

TAG Head总长度为10个字节。

头三个字节固定,表示'ID3'这三个字符

然后是两个字节表示标签版本号, 如$03 00,其中第一个字节表示主版本号,第二个自己表示副版本号

然后是1个字节的标志符,头三位有效,

     a位表示whether or not unsynchronisation is used

b位表示 whether or not the header is followed by an extended header

c位表示实验版

    然后是4个字节用来表示Tag的大小

每个字节的左起第一位无效。4个字节就还剩下28位,可表示256M

要得到size可以用如下方式计算:

(((B1 & 0x7f << 21)|(B2 & 0x7f <<14))|(B3 & 0x7f << 7)) |(B4 &0x7f)

B1表示size的第一个字节,0x7f为十六进制的7F

这里得到的size为整个标签的total size-10. 

The ID3v2 tag size is the size of the complete tag after unsychronisation, including padding, excluding the header but notexcluding the extended header (total tag size - 10)

如果有扩展头的话,扩展头结构如下:

Extended header size   $xx xx xx xx Extended Flags         $xx xxSize of padding        $xx xx xx xx


2.Frame head结构为:

Frame ID       $xx xx xx xx (four characters) Size           $xx xx xx xxFlags          $xx xx

同样此处的size表示的也是Frame的total size-10,

The size is calculated as frame size excluding frame header (frame size - 10).

Flag结构如下

%abc00000 %ijk00000

a - Tag alter preservation
This flag tells the software what to do with this frame if it is unknown and the tag is altered in any way. This applies to all kinds of alterations, including adding more padding and reordering the frames.
0    Frame should be preserved. 1    Frame should be discarded.
b - File alter preservation
This flag tells the software what to do with this frame if it is unknown and the file, excluding the tag, is altered. This does not apply when the audio is completely replaced with other audio data.
0    Frame should be preserved.1    Frame should be discarded.
c - Read only
This flag, if set, tells the software that the contents of this frame is intended to be read only. Changing the contents might break something, e.g. a signature. If the contents are changed, without knowledge in why the frame was flagged read only and without taking the proper means to compensate, e.g. recalculating the signature, the bit should be cleared.
i - Compression
This flag indicates whether or not the frame is compressed.
0    Frame is not compressed. 1    Frame is compressed using [#ZLIB zlib] with 4 bytes for 'decompressed size' appended to the frame header.
j - Encryption

This flag indicates wether or not the frame is enrypted. If set one byte indicating with which method it was encrypted will be appended to the frame header. See section 4.26. for more information about encryption method registration.

0    Frame is not encrypted. 1    Frame is encrypted.
k - Grouping identity
This flag indicates whether or not this frame belongs in a group with other frames. If set a group identifier byte is added to the frame header. Every frame with the same group identifier belongs to the same group.
0    Frame does not contain group information 1    Frame contains group information

3.Frame Body的第一个字节为字符编码字节。