MP3文件分析:TAG區


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.

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 , 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:

以上是MP3文件TAG區的文檔格式:
下面針對上面的說明,來訂製一個讀取TAG信息的類:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
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 "未知類型";
}

}
}


}
}