MP3文件分析:TAG区,C#小试牛刀!

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

SignLength
(bytes)
Position
(bytes)
Description
A3(0-2)Tag identification. Must contain 'TAG' if tag exists and is correct.
B30(3-32)Title
C30(33-62)Artist
D30(63-92)Album
E4(93-96)Year
F30(97-126)Comment
G1(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信息的类:

None.gif using  System;
None.gif
using  System.Text;
None.gif
None.gif
namespace  MP3Coder
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// clsMP3TAG 的摘要说明。
InBlock.gif    
/// 利用C#来解读MP3文件的TAG区信息。
InBlock.gif    
/// </summary>
InBlock.gif    
InBlock.gif    
/// 作者:任兀
InBlock.gif    
/// Nick Name:DSclub(兀儿 - 干部)
InBlock.gif    
/// QQ:9967030
InBlock.gif    
/// E-Mail:dsclub at 126.com
InBlock.gif    
/// MSN:dsclub at hotmail.com
InBlock.gif    
/// 版权声明:本代码只用于C#的学习交流。
ExpandedSubBlockEnd.gif    
/// 如果您想转载或将代码应用于您的作品中,请保留此信息。

InBlock.gif
InBlock.gif    
public class clsMP3TAG
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private byte[] TAGBody = new byte[128];
InBlock.gif
InBlock.gif        
private byte[] sTag = new byte[3];
InBlock.gif        
private byte[] sTitle = new byte[30];
InBlock.gif        
private byte[] sArtist = new byte[30];
InBlock.gif        
private byte[] sAlbum = new byte[30];
InBlock.gif        
private byte[] sYear = new byte[4];
InBlock.gif        
private byte[] sComment = new byte[30];
InBlock.gif        
private byte[] sGenre = new byte[1];
InBlock.gif        
InBlock.gif        System.Exception myException;
InBlock.gif
InBlock.gif        
public clsMP3TAG(byte[] TAG)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( TAG.Length != 128 )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                myException 
= new Exception("不是标准的 Mpeg-MP3 TAG 格式。\nTAG长度应该是 128 Byte。");
InBlock.gif                
throw(myException);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Array.Copy(TAG, 
0, sTag, 03);
InBlock.gif                
if!Encoding.Default.GetString(sTag).Equals("TAG") )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    myException 
= new Exception("不是标准的 Mpeg-MP3 TAG 格式。\nTAG位校验出错。");
InBlock.gif                    
throw(myException);
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                Array.Copy(TAG, 
3, sTitle, 030);
InBlock.gif                Array.Copy(TAG, 
33, sArtist, 030);
InBlock.gif                Array.Copy(TAG, 
63, sAlbum, 030);
InBlock.gif                Array.Copy(TAG, 
93, sYear, 04);
InBlock.gif                Array.Copy(TAG, 
97, sComment, 030);
InBlock.gif                Array.Copy(TAG, 
127, sGenre, 01);
InBlock.gif
InBlock.gif                    
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**///
InBlock.gif        
/// 以下是属性,只读
ExpandedSubBlockEnd.gif        
//

InBlock.gif        public string Title
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Encoding.Default.GetString(sTitle);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public string Artist
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Encoding.Default.GetString(sArtist);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public string Album
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Encoding.Default.GetString(sAlbum);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public string Year
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Encoding.Default.GetString(sYear);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public string Comment
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Encoding.Default.GetString(sComment);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public string Genre
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
switch(Convert.ToInt16(sGenre[0]))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
case 0return "Blues"case 20return "Alternative"case 40return "AlternRock"case 60return "Top 40"
InBlock.gif                    
case 1return "Classic Rock"case 21return "Ska"case 41return "Bass"case 61return "Christian Rap"
InBlock.gif                    
case 2return "Country"case 22return "Death Metal"case 42return "Soul"case 62return "Pop/Funk"
InBlock.gif                    
case 3return "Dance"case 23return "Pranks"case 43return "Punk"case 63return "Jungle"
InBlock.gif                    
case 4return "Disco"case 24return "Soundtrack"case 44return "Space"case 64return "Native American"
InBlock.gif                    
case 5return "Funk"case 25return "Euro-Techno"case 45return "Meditative"case 65return "Cabaret"
InBlock.gif                    
case 6return "Grunge"case 26return "Ambient"case 46return "Instrumental Pop"case 66return "New Wave"
InBlock.gif                    
case 7return "Hip-Hop"case 27return "Trip-Hop"case 47return "Instrumental Rock"case 67return "Psychadelic"
InBlock.gif                    
case 8return "Jazz"case 28return "Vocal"case 48return "Ethnic"case 68return "Rave"
InBlock.gif                    
case 9return "Metal"case 29return "Jazz+Funk"case 49return "Gothic"case 69return "Showtunes"
InBlock.gif                    
case 10return "New Age"case 30return "Fusion"case 50return "Darkwave"case 70return "Trailer"
InBlock.gif                    
case 11return "Oldies"case 31return "Trance"case 51return "Techno-Industrial"case 71return "Lo-Fi"
InBlock.gif                    
case 12return "Other"case 32return "Classical"case 52return "Electronic"case 72return "Tribal"
InBlock.gif                    
case 13return "Pop"case 33return "Instrumental"case 53return "Pop-Folk"case 73return "Acid Punk"
InBlock.gif                    
case 14return "R&B"case 34return "Acid"case 54return "Eurodance"case 74return "Acid Jazz"
InBlock.gif                    
case 15return "Rap"case 35return "House"case 55return "Dream"case 75return "Polka"
InBlock.gif                    
case 16return "Reggae"case 36return "Game"case 56return "Southern Rock"case 76return "Retro"
InBlock.gif                    
case 17return "Rock"case 37return "Sound Clip"case 57return "Comedy"case 77return "Musical"
InBlock.gif                    
case 18return "Techno"case 38return "Gospel"case 58return "Cult"case 78return "Rock & Roll"
InBlock.gif                    
case 19return "Industrial"case 39return "Noise"case 59return "Gangsta"case 79return "Hard Rock"
InBlock.gif
InBlock.gif
InBlock.gif                    
default:
InBlock.gif                        
return "未知类型";
ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值