注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

widebright的个人空间

// 编程和生活

 
 
 

日志

 
 

接上一篇: 编程保存IE缓存里面音乐文件为id3 tag信息中的歌名  

2011-03-06 00:22:31|  分类: 程序设计 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

以为很快可以做出来,结果还是花了很多时间去调试和修改bug,早知道就不去玩这个了!

接上一篇: 编程保存IE缓存里面音乐文件为id3 tag信息中的歌名 - widebright - widebright的个人空间

 

以后就方便了,在线听玩音乐后,想保存起来就拿出来,刷刷几下,比IE里面查看快多了,而且省去了手工改名的功夫。这个listview也还可以排序的,估计youku上看完电影,根据那个最后访问时间也可以把缓存下来的视频按顺序保存下来了。哈哈,谁也有这个需要的email我。

 

简单介绍一下原理:

遍历IE缓存中的文件,使用 WinINet 系列函数就可以了。http://msdn.microsoft.com/en-us/library/aa385473(v=VS.85).aspx

遍历 FindFirstUrlCacheEntry FindNextUrlCacheEntry

删除 DeleteUrlCacheEntry

读取缓存文件内容RetrieveUrlCacheEntryFile  RetrieveUrlCacheEntryStream  ReadUrlCacheEntryStream

我参考了http://www.codeproject.com/KB/cs/WebCacheTool.aspx  这个,在这个基础上做了修改,不过他那个代码应该在新的。net版本里面有些bug,比如api的long参数的长度问题。其实调用这个Windows api,还是用C/C++来的方便一些。C#这些要搞些封装,很容易声明参数长度那些出问题啦,麻烦死了!

mp3文件结尾的id3v1信息解析,也就是右键mp3文件 -》 属性-》摘要 那里看到的,里面包含歌名、专辑名、歌唱者等信息。可以参考官方格式的说明 http://www.id3.org/Implementations  ,我使了一下

ID3TagLib.Net http://id3tag.codeplex.com/

这个是可以的。

不过wma文件的里面的tag信息,不是使用的id3v1格式了,而是他asf文件格式本身就支持带有一些专辑名、歌曲标题之类的信息的。我在网上找到C#的一例子是:

http://www.codeproject.com/KB/audio-video/ProTagEditor.aspx

最后就是在这个基础上修改了一下,改成支持读取内存文件的。其实微软也有sdk做这个的,不过那东西太了吧,我们这种小东西没必要使用那个。

 

最后,完整的源码,想要的留个Email。都是些简单的东西,到处copy,copy!!!本来想传到sites.google.com 上去的,

╔═╤═╤═╤═╤═╤═╤═╤═╤═╤═╗
║ │ │ │ │ │ │!│生│了│结║
║ │ │ │ │ │ │ │活│了│果║
║ │ │ │ │ │ │ │在│!│也║
║ │ │ │ │ │ │ │天│!│被║
║ │ │ │ │ │ │ │朝│!│和║
║ │ │ │ │ │ │ │就│!│谐║
║ │ │ │ │ │ │ │是│ │了║
║ │ │ │ │ │ │ │幸│ │,║
║ │ │ │ │ │ │ │福│ │用║
║ │ │ │ │ │ │ │啊│ │不║
╚═╧═╧═╧═╧═╧═╧═╧═╧═╧═╝


 窗体代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;

 


using WebCacheTool;
using Id3Tag;
using Tags.ID3;
using Tags.ASF;
using Tags.Objects;
using Id3Tag.HighLevel;
using Id3Tag.HighLevel.Id3Frame;

namespace IECacheMusic
{

 


    public partial class Form1 : Form
    {
        int currentCol = -1;
        bool sort;

        public Form1()
        {
            InitializeComponent();
        }

        private string GetContent(IFrame frame)
        {
            if (frame != null)
            {
                var textFrame = frame as TextFrame;
                if (textFrame != null)
                {
                    return textFrame.Content;
                }
            }

            return String.Empty;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //System.Text.EncodingInfo[] info = System.Text.UnicodeEncoding.GetEncodings();

            //foreach (System.Text.EncodingInfo i in info)
            //{
            //try
            //{
            //    var manager = Id3TagFactory.CreateId3TagManager();
            //    //var tagV1 = manager.ReadV1Tag("C:\\Documents and Settings\\widebright\\桌面\\fxy.mp3", System.Text.Encoding.GetEncoding("GBK").CodePage);
            //    //var tagV1 = manager.ReadV1Tag("C:\\Documents and Settings\\widebright\\桌面\\等你爱我.mp3");
            //    // var tagV1 = manager.ReadV1Tag("C:\\Documents and Settings\\widebright\\桌面\\等你爱我.mp3", System.Text.Encoding.GetEncoding("GBK").CodePage);
            //    //var tagV1 = manager.ReadV1Tag("C:\\Documents and Settings\\widebright\\桌面\\等你爱我.mp3", System.Text.Encoding.ASCII.CodePage);
            //    var state = manager.GetTagsStatus("C:\\Documents and Settings\\widebright\\桌面\\fxy.mp3");
            //    var id3V1TagFound = state.Id3V1TagFound;

            //    if (id3V1TagFound)
            //    {
            //        var tagV1 = manager.ReadV1Tag("C:\\Documents and Settings\\widebright\\桌面\\fxy.mp3", System.Text.Encoding.GetEncoding("GBK").CodePage);
            //        if (tagV1.Artist == "陈明")
            //        {
            //            id3V1TagFound = id3V1TagFound;
            //        }
            //        //Title = v1Tag.Title,
            //        //Album = v1Tag.Album,
            //        //Artist = v1Tag.Artist,
            //        //Comment = v1Tag.Comment,
            //        //Genre = v1Tag.Genre,
            //        //GenreIdentifier = v1Tag.GenreIdentifier,
            //        //IsId3V1Dot1Compliant = v1Tag.IsId3V1Dot1Compliant

            //    }
            //    var id3V2TagFound = state.Id3V2TagFound;
            //    if (id3V2TagFound)
            //    {
            //        var tagV2 = manager.ReadV2Tag("C:\\Documents and Settings\\widebright\\桌面\\fxy.mp3", System.Text.Encoding.GetEncoding("GBK").CodePage);

            //        var Title = GetContent(tagV2.SearchFrame("TIT2"));
            //        var Album = GetContent(tagV2.SearchFrame("TALB"));
            //        var Artist = GetContent(tagV2.SearchFrame("TPE1"));
            //        var Track = GetContent(tagV2.SearchFrame("TRCK"));
            //        var Band = GetContent(tagV2.SearchFrame("TPE2"));
            //        var Year = GetContent(tagV2.SearchFrame("TYER"));
            //        var Encoder = GetContent(tagV2.SearchFrame("TENC"));


            //    }

            //    //codepage=936 简体中文GBK


            //}
            //catch (Exception ex)
            //{

            //}

            // }


            String urlPattern = comboBox1.Text;
            ArrayList results = WinInetAPI.FindUrlCacheEntries(urlPattern);

            listView1.Items.Clear();

            foreach (CacheFileInfo entry in results)
            {

                String title = "";
                String album = "";

                ListViewItem item1 = new ListViewItem(title, 0);
                // Place a check mark next to the item.
                item1.Checked = true;
                item1.SubItems.Add(album);

                double size = entry.info.dwSizeLow;
                size /= (1024 * 1024);

                item1.SubItems.Add(size.ToString("#0.00") + " MB");
                item1.SubItems.Add(Win32API.ToStringFromFileTime(entry.info.LastAccessTime));

                string localFilename = System.IO.Path.GetFileName(entry.localFilename);
                item1.SubItems.Add(localFilename);
                item1.SubItems.Add(entry.url);

                listView1.Items.Add(item1);

            }

            Thread thread = new Thread(new ParameterizedThreadStart(Id3Query));
            thread.Start(listView1);

        }

        void Id3Query(object mylist)
        {
            ListView list = (ListView)mylist;

            foreach (ListViewItem item in list.Items)
            {
                string localFilename = item.SubItems[4].Text;
                string url = item.SubItems[5].Text;
                Stream content = WinInetAPI.RetrieveUrlCacheEntryContents(url);
                if (content.Length < 128)  //读不到文件
                {
                    continue;
                }
                String title = "";
                String album = "";
                try
                {
                    //codepage=936 简体中文GBK ,两个库的Id3都可以用的,
                    var manager = Id3TagFactory.CreateId3TagManager();
                    //var tagV1 = manager.ReadV1Tag(content, System.Text.Encoding.GetEncoding("GBK").CodePage);
                    //title = tagV1.Title + "-" + tagV1.Artist;
                    //album = tagV1.Album;

                    var state = manager.GetTagsStatus(content);
                    if (state.Id3V1TagFound)
                    {
                        var tagV1 = manager.ReadV1Tag(content, System.Text.Encoding.GetEncoding("GBK").CodePage);
                        title = tagV1.Title + "-" + tagV1.Artist;
                        album = tagV1.Album;
                    }else if (state.Id3V2TagFound)
                    {
                        //
                        var tagV2 = manager.ReadV2Tag(content, System.Text.Encoding.GetEncoding("GBK").CodePage);
                        var Title = GetContent(tagV2.SearchFrame("TIT2"));
                        var Album = GetContent(tagV2.SearchFrame("TALB"));
                        var Artist = GetContent(tagV2.SearchFrame("TPE1"));
                        //var Track = GetContent(tagV2.SearchFrame("TRCK"));
                        //var Band = GetContent(tagV2.SearchFrame("TPE2"));
                        //var Year = GetContent(tagV2.SearchFrame("TYER"));
                        //var Encoder = GetContent(tagV2.SearchFrame("TENC"));
                       
                        if (Title != String.Empty || Artist !=  String.Empty){
                       
                             title = Title + "-" + Artist;
                        }
                        album = Album;
                      
                    }
                   
                    //这个id3v1代码少一点 .不过我没有把它id3v2支持的代码搞过来的,上面那个支持
                    //ID3v1 id3 = new ID3v1(content, System.Text.Encoding.GetEncoding("GBK").CodePage);
                    //if (id3.HaveTag)
                    //{
                    //    title = id3.Title + "-" + id3.Artist;
                    //    album = id3.Album;
                    //}

                    if (localFilename.EndsWith(".wma"))
                    {
                        ASFTagInfo info = new ASFTagInfo(content);
                        //info.ContentBranding.
                        if (info.ContentDescription.Title != String.Empty || info.ContentDescription.Author != String.Empty)
                        {
                            title = info.ContentDescription.Title + "-" + info.ContentDescription.Author;

                            Descriptor des = (Descriptor)info.ExContentDescription.GetValue("WM/AlbumTitle"); //"WM/Genre"  "WM/Year"  "WM/TrackNumber" "WM/Track"
                            album = (string)des.Value;
                        }
                    }

                }
                catch (Exception ex)
                {
                    title = "";
                    album = "";
                }

                item.SubItems[0].Text = title;
                item.SubItems[1].Text = album;
                content.Close();
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
            // Set the view to show details.
            listView1.View = View.Details;
            // Allow the user to edit item text.
            listView1.LabelEdit = false;
            // Allow the user to rearrange columns.
            listView1.AllowColumnReorder = true;
            // Display check boxes.
            listView1.CheckBoxes = true;
            // Select the item and subitems when selection is made.
            listView1.FullRowSelect = true;
            // Display grid lines.
            listView1.GridLines = true;
            // Sort the items in the list in ascending order.
            listView1.Sorting = SortOrder.Ascending;

            Control.CheckForIllegalCrossThreadCalls = false;  //允许在后台线程访问 listview空间,不过这样做不安全啊

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == folderBrowserDialog1.ShowDialog())
            {

                string path = folderBrowserDialog1.SelectedPath;
                foreach (ListViewItem item in listView1.Items)
                {
                    if (item.Checked == true)
                    {
                        string url = item.SubItems[5].Text;
                        string filename = item.SubItems[0].Text;
                        filename.Trim();
                        string localFilename = item.SubItems[4].Text;
                        string ext = Path.GetExtension(localFilename);
                        if (filename.Length < 2)
                        {
                            localFilename = path + "\\" + localFilename;
                        }
                        else
                        {
                            localFilename = path + "\\" + filename + ext;
                        }
                        try
                        {
                            FileStream file = new FileStream(localFilename, FileMode.CreateNew);
                            MemoryStream content = (MemoryStream)WinInetAPI.RetrieveUrlCacheEntryContents(url);
                            if (content.Length < 128)  //读不到文件
                            {
                                content.Close();
                                file.Close();
                                continue;
                            }
                            file.Write(content.GetBuffer(), 0, (int)content.Length);
                            content.Close();
                            file.Close();

                        }
                        catch (IOException ex)
                        {
                            MessageBox.Show(localFilename + "文件已经存在,取消保存!", "你真是笨蛋啊!");
                        }
                    }
                }

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.Items)
            {
                if (item.Checked == true)
                {
                    string localFilename = item.SubItems[4].Text;
                    string url = item.SubItems[5].Text;
                    if (WinInetAPI.DeleteFromUrlCache(url) > 0)
                    {
                        listView1.Items.Remove(item);
                    }
                }
            }
        }

        private void listview_columnclick(object sender, ColumnClickEventArgs e)
        {
            string Asc = ((char)0x25bc).ToString().PadLeft(4, ' ');
            string Des = ((char)0x25b2).ToString().PadLeft(4, ' ');

            if (sort == false)
            {
                sort = true;
                string oldStr = this.listView1.Columns[e.Column].Text.TrimEnd((char)0x25bc, (char)0x25b2, ' ');
                this.listView1.Columns[e.Column].Text = oldStr + Des;
            }
            else if (sort == true)
            {
                sort = false;
                string oldStr = this.listView1.Columns[e.Column].Text.TrimEnd((char)0x25bc, (char)0x25b2, ' ');
                this.listView1.Columns[e.Column].Text = oldStr + Asc;
            }

            listView1.ListViewItemSorter = new ListViewItemComparer(e.Column, sort);
            this.listView1.Sort();
            int rowCount = this.listView1.Items.Count;
            if (currentCol != -1)
            {
                for (int i = 0; i < rowCount; i++)
                {
                    this.listView1.Items[i].UseItemStyleForSubItems = false;
                    this.listView1.Items[i].SubItems[currentCol].BackColor = Color.White;

                    if (e.Column != currentCol)
                        this.listView1.Columns[currentCol].Text = this.listView1.Columns[currentCol].Text.TrimEnd((char)0x25bc, (char)0x25b2, ' ');
                }
            }

            for (int i = 0; i < rowCount; i++)
            {
                this.listView1.Items[i].UseItemStyleForSubItems = false;
                this.listView1.Items[i].SubItems[e.Column].BackColor = Color.WhiteSmoke;
                currentCol = e.Column;
            }

        }


    }
}

  评论这张
 
阅读(826)| 评论(6)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017