The TAG is used to describe the MPEG Audio file. It contains information about artist, title, album, publishing year and genre. There is some extra space for comments. It is exactly 128 bytes long and is located at very end of the audio data. You can get it by reading the last 128 bytes of the MPEG audio file.
AAABBBBB BBBBBBBB BBBBBBBB BBBBBBBB
BCCCCCCC CCCCCCCC CCCCCCCC CCCCCCCD
DDDDDDDD DDDDDDDD DDDDDDDD DDDDDEEE
EFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFG
Sign | Length (bytes) |
Position (bytes) |
Description |
A | 3 | (0-2) | Tag identification. Must contain 'TAG' if tag exists and is correct. |
B | 30 | (3-32) | Title |
C | 30 | (33-62) | Artist |
D | 30 | (63-92) | Album |
E | 4 | (93-96) | Year |
F | 30 | (97-126) | Comment |
G | 1 | (127) | Genre |
The specification asks for all fields to be padded with null character (ASCII 0). However, not all applications respect this (an example is WinAmp which pads fields with <space>, ASCII 32).
There is a small change proposed in MP3v1.1 structure. The last byte of the Comment field may be used to specify the track number of a song in an album. It should contain a null character (ASCII 0) if the information is unknown.
Genre is a numeric field which may have one of the following values:
0 | 'Blues' | 20 | 'Alternative' | 40 | 'AlternRock' | 60 | 'Top 40' |
1 | 'Classic Rock' | 21 | 'Ska' | 41 | 'Bass' | 61 | 'Christian Rap' |
2 | 'Country' | 22 | 'Death Metal' | 42 | 'Soul' | 62 | 'Pop/Funk' |
3 | 'Dance' | 23 | 'Pranks' | 43 | 'Punk' | 63 | 'Jungle' |
4 | 'Disco' | 24 | 'Soundtrack' | 44 | 'Space' | 64 | 'Native American' |
5 | 'Funk' | 25 | 'Euro-Techno' | 45 | 'Meditative' | 65 | 'Cabaret' |
6 | 'Grunge' | 26 | 'Ambient' | 46 | 'Instrumental Pop' | 66 | 'New Wave' |
7 | 'Hip-Hop' | 27 | 'Trip-Hop' | 47 | 'Instrumental Rock' | 67 | 'Psychadelic' |
8 | 'Jazz' | 28 | 'Vocal' | 48 | 'Ethnic' | 68 | 'Rave' |
9 | 'Metal' | 29 | 'Jazz+Funk' | 49 | 'Gothic' | 69 | 'Showtunes' |
10 | 'New Age' | 30 | 'Fusion' | 50 | 'Darkwave' | 70 | 'Trailer' |
11 | 'Oldies' | 31 | 'Trance' | 51 | 'Techno-Industrial' | 71 | 'Lo-Fi' |
12 | 'Other' | 32 | 'Classical' | 52 | 'Electronic' | 72 | 'Tribal' |
13 | 'Pop' | 33 | 'Instrumental' | 53 | 'Pop-Folk' | 73 | 'Acid Punk' |
14 | 'R&B' | 34 | 'Acid' | 54 | 'Eurodance' | 74 | 'Acid Jazz' |
15 | 'Rap' | 35 | 'House' | 55 | 'Dream' | 75 | 'Polka' |
16 | 'Reggae' | 36 | 'Game' | 56 | 'Southern Rock' | 76 | 'Retro' |
17 | 'Rock' | 37 | 'Sound Clip' | 57 | 'Comedy' | 77 | 'Musical' |
18 | 'Techno' | 38 | 'Gospel' | 58 | 'Cult' | 78 | 'Rock & Roll' |
19 | 'Industrial' | 39 | 'Noise' | 59 | 'Gangsta' | 79 | 'Hard Rock' |
Any other value should be considered as 'Unknown' |
以上是MP3文件TAG区的文档格式:
下面针对上面的说明,来订制一个读取TAG信息的类:
using System.Text;
namespace MP3Coder
{
/// <summary>
/// clsMP3TAG 的摘要说明。
/// 利用C#来解读MP3文件的TAG区信息。
/// </summary>
/// 作者:任兀
/// Nick Name:DSclub(兀儿 - 干部)
/// QQ:9967030
/// E-Mail:dsclub at 126.com
/// MSN:dsclub at hotmail.com
/// 版权声明:本代码只用于C#的学习交流。
/// 如果您想转载或将代码应用于您的作品中,请保留此信息。
public class clsMP3TAG
{
private byte[] TAGBody = new byte[128];
private byte[] sTag = new byte[3];
private byte[] sTitle = new byte[30];
private byte[] sArtist = new byte[30];
private byte[] sAlbum = new byte[30];
private byte[] sYear = new byte[4];
private byte[] sComment = new byte[30];
private byte[] sGenre = new byte[1];
System.Exception myException;
public clsMP3TAG(byte[] TAG)
{
if( TAG.Length != 128 )
{
myException = new Exception("不是标准的 Mpeg-MP3 TAG 格式。\nTAG长度应该是 128 Byte。");
throw(myException);
}
else
{
Array.Copy(TAG, 0, sTag, 0, 3);
if( !Encoding.Default.GetString(sTag).Equals("TAG") )
{
myException = new Exception("不是标准的 Mpeg-MP3 TAG 格式。\nTAG位校验出错。");
throw(myException);
}
Array.Copy(TAG, 3, sTitle, 0, 30);
Array.Copy(TAG, 33, sArtist, 0, 30);
Array.Copy(TAG, 63, sAlbum, 0, 30);
Array.Copy(TAG, 93, sYear, 0, 4);
Array.Copy(TAG, 97, sComment, 0, 30);
Array.Copy(TAG, 127, sGenre, 0, 1);
}
}
//////////////////////////////////////////////////////
/// 以下是属性,只读
//////////////////////////////////////////////////////
public string Title
{
get
{
return Encoding.Default.GetString(sTitle);
}
}
public string Artist
{
get
{
return Encoding.Default.GetString(sArtist);
}
}
public string Album
{
get
{
return Encoding.Default.GetString(sAlbum);
}
}
public string Year
{
get
{
return Encoding.Default.GetString(sYear);
}
}
public string Comment
{
get
{
return Encoding.Default.GetString(sComment);
}
}
public string Genre
{
get
{
switch(Convert.ToInt16(sGenre[0]))
{
case 0: return "Blues"; case 20: return "Alternative"; case 40: return "AlternRock"; case 60: return "Top 40";
case 1: return "Classic Rock"; case 21: return "Ska"; case 41: return "Bass"; case 61: return "Christian Rap";
case 2: return "Country"; case 22: return "Death Metal"; case 42: return "Soul"; case 62: return "Pop/Funk";
case 3: return "Dance"; case 23: return "Pranks"; case 43: return "Punk"; case 63: return "Jungle";
case 4: return "Disco"; case 24: return "Soundtrack"; case 44: return "Space"; case 64: return "Native American";
case 5: return "Funk"; case 25: return "Euro-Techno"; case 45: return "Meditative"; case 65: return "Cabaret";
case 6: return "Grunge"; case 26: return "Ambient"; case 46: return "Instrumental Pop"; case 66: return "New Wave";
case 7: return "Hip-Hop"; case 27: return "Trip-Hop"; case 47: return "Instrumental Rock"; case 67: return "Psychadelic";
case 8: return "Jazz"; case 28: return "Vocal"; case 48: return "Ethnic"; case 68: return "Rave";
case 9: return "Metal"; case 29: return "Jazz+Funk"; case 49: return "Gothic"; case 69: return "Showtunes";
case 10: return "New Age"; case 30: return "Fusion"; case 50: return "Darkwave"; case 70: return "Trailer";
case 11: return "Oldies"; case 31: return "Trance"; case 51: return "Techno-Industrial"; case 71: return "Lo-Fi";
case 12: return "Other"; case 32: return "Classical"; case 52: return "Electronic"; case 72: return "Tribal";
case 13: return "Pop"; case 33: return "Instrumental"; case 53: return "Pop-Folk"; case 73: return "Acid Punk";
case 14: return "R&B"; case 34: return "Acid"; case 54: return "Eurodance"; case 74: return "Acid Jazz";
case 15: return "Rap"; case 35: return "House"; case 55: return "Dream"; case 75: return "Polka";
case 16: return "Reggae"; case 36: return "Game"; case 56: return "Southern Rock"; case 76: return "Retro";
case 17: return "Rock"; case 37: return "Sound Clip"; case 57: return "Comedy"; case 77: return "Musical";
case 18: return "Techno"; case 38: return "Gospel"; case 58: return "Cult"; case 78: return "Rock & Roll";
case 19: return "Industrial"; case 39: return "Noise"; case 59: return "Gangsta"; case 79: return "Hard Rock";
default:
return "未知类型";
}
}
}
}
}