从MP3中提取歌曲信息(C#) 选择自 ufoer23 的 Blog

从MP3中提取歌曲信息(C#)     选择自 ufoer23 的 Blog  
关键字   从MP3中提取歌曲信息(C#) 
出处     
 从MP3中提取歌曲信息 
 
作者:game.19xz 来源:19xz.com  
从MP3中提取歌曲信息

       一首MP3歌曲除了音乐信息外,还包含了如歌名、演唱者等信息,当我们用winamp软件听音乐时,播放清单就自动将这些信息读出来。大部分人都喜欢从网上下载音乐,但下载下来的MP3文件名都是文件上传系统自动取名的,和歌曲本身根本不相符,所以,给用户带来了很大的麻烦。但是,懒人有懒人的做法,我们何不自己写一个程序,将歌曲信息自动读出来并为MP3文件自动更名呢?

        下面我就以C#为工具,把开发过程写出来。

        一首MP3的额外信息存放在文件的最后面,共占128个字节,其中包括以下的内容(我们定义一个结构说明):

     

ContractedBlock.gif ExpandedBlockStart.gif
None.gif  public struct Mp3Info
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif            
public string identify;//TAG,三个字节
InBlock.gif
            public string Title;//歌曲名,30个字节
InBlock.gif
            public string Artist;//歌手名,30个字节
InBlock.gif
            public string Album;//所属唱片,30个字节
InBlock.gif
            public string Year;//年,4个字符
InBlock.gif
            public string Comment;//注释,28个字节 
InBlock.gif
            public char reserved1;//保留位,一个字节
InBlock.gif
            public char reserved2;//保留位,一个字节
InBlock.gif
            public char reserved3;//保留位,一个字节
ExpandedBlockEnd.gif
        }

None.gif
None.gif       所以,我们只要把MP3文件的最后128个字节分段读出来并保存到该结构里就可以了。函数定义如下:
None.gif
ExpandedBlockStart.gifContractedBlock.gif          
/**//// <summary>
InBlock.gif        
/// 获取MP3文件最后128个字节
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="FileName">文件名</param>
InBlock.gif
ExpandedBlockEnd.gif        
/// <returns>返回字节数组</returns>

None.gif
None.gif        
private byte[] getLast128(string FileName)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            FileStream fs 
= new FileStream(FileName,FileMode.Open,FileAccess.Read);
InBlock.gif            Stream stream 
= fs;
InBlock.gif             stream.Seek(
-128,SeekOrigin.End); 
InBlock.gif            
const int seekPos = 128;
InBlock.gif            
int rl = 0;
InBlock.gif            
byte[] Info = new byte[seekPos];
InBlock.gif            rl 
= stream.Read(Info,0,seekPos); 
InBlock.gif            fs.Close();
InBlock.gif            stream.Close(); 
InBlock.gif            
return Info;
ExpandedBlockEnd.gif        }
 
None.gif
None.gif       再对上面返回的字节数组分段取出,并保存到Mp3Info结构中返回。
None.gif
ExpandedBlockStart.gifContractedBlock.gif              
/**//// <summary>
InBlock.gif        
/// 获取MP3歌曲的相关信息
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name = "Info">从MP3文件中截取的二进制信息</param>
ExpandedBlockEnd.gif        
/// <returns>返回一个Mp3Info结构</returns>

None.gif
None.gif        
private Mp3Info getMp3Info(byte[] Info)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            Mp3Info mp3Info 
= new Mp3Info();
InBlock.gif            
string str = null;
InBlock.gif            
int i;
InBlock.gif
InBlock.gif            
int position = 0;//循环的起始值
InBlock.gif
             int currentIndex = 0;//Info的当前索引值
InBlock.gif            
//获取TAG标识
InBlock.gif

InBlock.gif            
for(i = currentIndex;i<currentIndex+3;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif              str 
= str+(char)Info[i];
InBlock.gif              position
++;
ExpandedSubBlockEnd.gif          }

InBlock.gif            currentIndex 
= position;
InBlock.gif            mp3Info.identify 
= str;
InBlock.gif            
//获取歌名
InBlock.gif
            str = null;
InBlock.gif            
byte[] bytTitle = new byte[30];//将歌名部分读到一个单独的数组中
InBlock.gif

InBlock.gif            
int j = 0;
InBlock.gif            
for(i = currentIndex;i<currentIndex+30;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif{
InBlock.gif                bytTitle[j] 
= Info[i];    
InBlock.gif                position
++;
InBlock.gif                j
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            currentIndex 
= position;
InBlock.gif            mp3Info.Title 
= this.byteToString(bytTitle);
InBlock.gif
InBlock.gif            
//获取歌手名
InBlock.gif
            str = null;
InBlock.gif            j 
= 0;
InBlock.gif
InBlock.gif            
byte[] bytArtist = new byte[30];//将歌手名部分读到一个单独的数组中
InBlock.gif

InBlock.gif            
for(i = currentIndex;i<currentIndex+30;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                bytArtist[j] 
= Info[i];    
InBlock.gif                position
++;
InBlock.gif                j
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            currentIndex 
= position;
InBlock.gif            mp3Info.Artist 
= this.byteToString(bytArtist);
InBlock.gif            
//获取唱片名
InBlock.gif
            str = null;
InBlock.gif            j 
= 0;
InBlock.gif            
byte[] bytAlbum = new byte[30];//将唱片名部分读到一个单独的数组中
InBlock.gif

InBlock.gif            
for(i = currentIndex;i<currentIndex+30;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                bytAlbum[j] 
= Info[i];    
InBlock.gif                position
++;
InBlock.gif                j
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            currentIndex 
= position;
InBlock.gif
InBlock.gif            mp3Info.Album 
= this.byteToString(bytAlbum);
InBlock.gif
InBlock.gif            
//获取年
InBlock.gif

InBlock.gif            str 
= null;
InBlock.gif          j 
= 0;
InBlock.gif
InBlock.gif            
byte[] bytYear = new byte[4];//将年部分读到一个单独的数组中
InBlock.gif

InBlock.gif            
for(i = currentIndex;i<currentIndex+4;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                bytYear[j] 
= Info[i];  
InBlock.gif                position
++;
InBlock.gif                j
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            currentIndex 
= position;
InBlock.gif            mp3Info.Year 
= this.byteToString(bytYear);
InBlock.gif
InBlock.gif            
//获取注释
InBlock.gif
            str = null;
InBlock.gif            j 
= 0;
InBlock.gif            
byte[] bytComment = new byte[28];//将注释部分读到一个单独的数组中
InBlock.gif

InBlock.gif            
for(i = currentIndex;i<currentIndex+25;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                bytComment[j] 
= Info[i];    
InBlock.gif                position
++;
InBlock.gif                j
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif            currentIndex 
= position;
InBlock.gif            mp3Info.Comment 
= this.byteToString(bytComment);
InBlock.gif
InBlock.gif            
//以下获取保留位
InBlock.gif
            mp3Info.reserved1 = (char)Info[++position];
InBlock.gif            mp3Info.reserved2 
= (char)Info[++position];
InBlock.gif            mp3Info.reserved3 
= (char)Info[++position];
InBlock.gif            
return mp3Info;
ExpandedBlockEnd.gif        }

None.gif       上面程序用到下面的方法:
ExpandedBlockStart.gifContractedBlock.gif              
/**//// <summary>
InBlock.gif        
/// 将字节数组转换成字符串
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name = "b">字节数组</param>
ExpandedBlockEnd.gif        
/// <returns>返回转换后的字符串</returns>

None.gif        private string byteToString(byte[] b)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            Encoding enc 
= Encoding.GetEncoding("GB2312");
InBlock.gif            
string str = enc.GetString(b);
InBlock.gif            str 
= str.Substring(0,str.IndexOf('\0'>= 0 ? str.IndexOf('\0') : str.Length);//去掉无用字符
InBlock.gif
            return str;
ExpandedBlockEnd.gif        }

None.gif
None.gif
None.gif       改名怎么办呢?我们按(演唱者)歌名 的格式对歌曲进行改名,程序如下:
None.gif
ExpandedBlockStart.gifContractedBlock.gif       
/**//// <summary>
InBlock.gif        
/// 更改文件名
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="filePath">文件名</param>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif
None.gif        
private bool ReName(string filePath)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
if(File.Exists(filePath))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Mp3Info mp3Info 
= new Mp3Info();
InBlock.gif                mp3Info 
= this.getMp3Info(this.getLast128(filePath));//读出文件信息
InBlock.gif

InBlock.gif                mp3Info.Artist 
= this.DeleteNotvalue(mp3Info.Artist);
InBlock.gif                mp3Info.Title 
= this.DeleteNotvalue(mp3Info.Title);
InBlock.gif                
if(mp3Info.Artist.Trim().Length==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    mp3Info.Artist
="未命名";
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if(mp3Info.Title.Trim().Length==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    mp3Info.Title
="未知名歌曲";
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//更名
InBlock.gif
                    File.Move(filePath,filePath.Substring(0,filePath.ToLower().LastIndexOf("\\")).Trim() + "\\" + "(" + mp3Info.Artist.Trim() + ")" +mp3Info.Title.Trim() + ".mp3");
InBlock.gif                    
return true;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch(Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{         
InBlock.gif              
return false; 
ExpandedSubBlockEnd.gif                 }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif       }

None.gif
None.gif

 

呵,思路就是这样了,如果有问题或者需要源码请发邮件至:lifenote@21cn.com索取。
 


作者Blog:http://blog.youkuaiyun.com/ufoer23/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值