首页 > 开发 > 综合 > 正文

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

2024-07-21 02:26:25
字体:
来源:转载
供稿:网友

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 "未知类型";
                }


            }

        }

    

    }

}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表