【笔记】解析MP3文件信息

在写有关音乐播放的程序的时候有时需要解析文件的一些基本信息,例如MP3格式的文件中的 艺术家,专辑,歌曲名,比特率,专辑图片等等

直接对MP3文件编码进行分析的过程比较复杂,这里介绍一个库的使用,通过这个库可以很方便的分析出MP3文件的信息

该类库项目地址:http://www.codeproject.com/Articles/17890/Do-Anything-With-ID3

得到ID3.dll,引用到项目中

string path = @"G:\Music\周杰伦-轨迹.mp3";
ID3Info info = new ID3Info(path, true);            //第二个参数表示是否加载数据

ID3Info类有四个属性

  FilePath,FileName,ID3v1Info, ID3v2Info

  其中ID3v1Info有以下成员,具体说明可以参考

http://baike.baidu.com/view/66078.htm#3

        string title = info.ID3v1Info.Title;        //标题
        string artist = info.ID3v1Info.Artist;        //作者
        string album = info.ID3v1Info.Album;        //专辑
        string comment = info.ID3v1Info.Comment;    //备注
        string genre = info.ID3v1Info.Genre;        //类型
        string year = info.ID3v1Info.Year            //出品年代

  ID3v2Info属性包含的数据比较复杂,一般用函数对其进行操作,主要有

  属性:

    AttachedPictureFrames  //可以MP3文件中含有的图片

  函数:    GetTextFrame  SetTextFrame  //获取歌曲信息

        string title = info.ID3v2Info.GetTextFrame("TIT2");
        string artist = info.ID3v2Info.GetTextFrame("TPE1");
        string album = info.ID3v2Info.GetTextFrame("TALB");
        string trckNumber = info.ID3v2Info.GetTextFrame("TRCK");
        string year = info.ID3v2Info.GetTextFrame("TYER");
        string genre = info.ID3v2Info.GetTextFrame("TCON");
        string cmm = info.ID3v2Info.GetTextFrame("COMM");
        
        info.ID3v2Info.SetTextFrame("TIT2", "歌曲标题");
        info.ID3v2Info.SetTextFrame("TPE1", "艺术家");
        info.ID3v2Info.SetTextFrame("TALB", "专辑");
        info.ID3v2Info.SetTextFrame("COMM", "备注");
        
        // 创建新封面
        string picuter = @"C:\Users\bomo\Desktop\pic.jpg";
        AttachedPictureFrame pic = new AttachedPictureFrame(FrameFlags.Compression, "cover.jpg", TextEncodings.UTF_16, "", 
            AttachedPictureFrame.PictureTypes.Other, 
            new System.IO.MemoryStream(File.ReadAllBytes(picture)));
        
        //添加封面到MP3文件中
        info.ID3v2Info.AttachedPictureFrames.Add(pic);
        
        //读取图片
        foreach (AttachedPictureFrame frame in frames)
        {
            MemoryStream stream = frame.Data;
            string saveFile = @"C:\Users\bomo\Desktop\pic2.jpg";

            using (FileStream fs = new FileStream(saveFile, FileMode.OpenOrCreate, FileAccess.Write))
            {
                int b;
                while((b = stream.ReadByte()) != -1)
                {
                    fs.WriteByte((byte)b);
                }
            }
        }
        info.save();

注意,对文件修改后一定要调用save()方法,才会使修改生效

其他一些属性和方法不是很常用,更多信息可以参阅:http://id3.org/

还有一个解析文件的时间长度,由于MP3文件本身没有存放时间的信息,所以这里我们使用微软提供的API,首先引入两个函数

        [DllImport("Kernel32", CharSet = CharSet.Auto)]
        static extern Int32 GetShortPathName(String path, StringBuilder shortPath, Int32 shortPathLength);

        [DllImport("winmm.dll")]
        public static extern int mciSendString(string m_strCmd, StringBuilder m_strReceive, int m_v1, int m_v2); 

接下来是计算出歌曲的时间,直接给出c#代码

        public string GetLengthString(string path)
        {
            StringBuilder buf = new StringBuilder(80);
            GetShortPathName(@path, buf, buf.Capacity);
            string shortname = buf.ToString();

            buf = new StringBuilder(80);
            mciSendString("close all", buf, buf.Capacity, 0);
            mciSendString("open " + shortname + " alias song", buf, buf.Capacity, 0);
            mciSendString("status song length", buf, buf.Capacity, 0);


            int len = (int)Convert.ToDouble(buf.ToString()) / 1000;        //
            TimeSpan ts = new TimeSpan(0, 0, len);
            int minuts = ts.Minutes;
            int second = ts.Seconds;

            return minuts.ToString("d2") + ":" + second.ToString("d2");
        }

ID3库文件 https://files.cnblogs.com/bomo/ID3.zip

原文地址:https://www.cnblogs.com/bomo/p/2809354.html