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

80酷酷网    80kuku.com

  

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信息的类:

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, 03);
                
if!Encoding.Default.GetString(sTag).Equals("TAG") )
                
{
                    myException 
= new Exception("不是标准的 Mpeg-MP3 TAG 格式。\nTAG位校验出错。");
                    
throw(myException);
                }


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

                    
            }

        }


        
/**///////////////////////////////////////////////////////
        
/// 以下是属性,只读
        
//////////////////////////////////////////////////////

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


                    
default:
                        
return "未知类型";
                }


            }

        }

    

    }

}

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: