利用 Windows API Code Pack 修改音乐的 ID3 信息

简介:

朋友由于抠门 SD 卡买小了,结果音乐太多放不下,又不舍得再买新卡,不得已决定重新转码,把音乐码率压低一点,牺牲点音质来换空间(用某些人的话说,反正不是搞音乐的,听不出差别)…

结果千千静听(百度音乐)转码后会把音乐 ID3 信息里的标题当文件名…(PS:怀念当年的 千千静听…)

结果成了这个模样:

image  image

 

由于数量较多,收工改会死人的..

所以只好想办法用程序来批量改

 

找到了园友的老文章:http://www.cnblogs.com/TianFang/archive/2009/09/27/1574722.html

 

于是决定用 Windows API Code Pack

不过 MS 网站已经木有 Windows API Code Pack 的链接了

不过找起来也不麻烦

 

找到后新建工程,添加引用这两个 dll 就可以了

image

 

然后在工程里新建类 MediaTags,写入内容如下 :

    class MediaTags
    {
        #region Mp3文件属性
        /// <summary>
        /// 标题
        /// </summary>
        [MediaProperty("Title")]
        public string Title { get; set; }
 
        /// <summary>
        /// 子标题
        /// </summary>
        [MediaProperty("Media.SubTitle")]
        public string SubTitle { get; set; }
 
        /// <summary>
        /// 星级
        /// </summary>
        [MediaProperty("Rating")]
        public uint? Rating { get; set; }
 
        /// <summary>
        /// 备注
        /// </summary>
        [MediaProperty("Comment")]
        public string Comment { get; set; }
 
        /// <summary>
        /// 艺术家
        /// </summary>
        [MediaProperty("Author")]
        public string Author { get; set; }
 
        /// <summary>
        /// 唱片集
        /// </summary>
        [MediaProperty("Music.AlbumTitle")]
        public string AlbumTitle { get; set; }
 
        /// <summary>
        /// 唱片集艺术家
        /// </summary>
        [MediaProperty("Music.AlbumArtist")]
        public string AlbumArtist { get; set; }
 
        /// <summary>
        /// 年
        /// </summary>
        [MediaProperty("Media.Year")]
        public uint? Year { get; set; }
 
        /// <summary>
        /// 流派
        /// </summary>
        [MediaProperty("Music.Genre")]
        public string Genre { get; set; }
 
        /// <summary>
        /// #
        /// </summary>
        [MediaProperty("Music.TrackNumber")]
        public uint? TrackNumber { get; set; }
 
        /// <summary>
        /// 播放时间
        /// </summary>
        [MediaProperty("Media.Duration")]
        public string Duration { get; private set; }
 
        /// <summary>
        /// 比特率
        /// </summary>
        [MediaProperty("Audio.EncodingBitrate")]
        public string BitRate { get; private set; }
        #endregion
 
        public MediaTags(string mediaPath)
        {
            //var obj = ShellObject.FromParsingName(mp3Path);   //缩略图,只读
            //obj.Thumbnail.Bitmap.Save(@"R:\2.jpg");
 
            Init(mediaPath);
        }
 
        void Init(string mediaPath)
        {
            using (var obj = ShellObject.FromParsingName(mediaPath))
            {
                var mediaInfo = obj.Properties;
                foreach (var properItem in this.GetType().GetProperties())
                {
                    var mp3Att = properItem.GetCustomAttributes(typeof(MediaPropertyAttribute), false).FirstOrDefault();
                    var shellProper = mediaInfo.GetProperty("System." + mp3Att);
                    var value = shellProper == null ? null : shellProper.ValueAsObject;
 
                    if (value == null)
                    {
                        continue;
                    }
 
                    if (shellProper.ValueType == typeof(string[]))    //艺术家,流派等多值属性
                    {
                        properItem.SetValue(this, string.Join(";", value as string[]), null);
                    }
                    else if (properItem.PropertyType != shellProper.ValueType)    //一些只读属性,类型不是string,但作为string输出,避免转换 如播放时间,比特率等
                    {
                        properItem.SetValue(this, value == null ? "" : shellProper.FormatForDisplay(PropertyDescriptionFormatOptions.None), null);
                    }
                    else
                    {
                        properItem.SetValue(this, value, null);
                    }
                }
            }
        }
 
        public void Commit(string mp3Path)
        {
            var old = new MediaTags(mp3Path);
 
            using (var obj = ShellObject.FromParsingName(mp3Path))
            {
                var mediaInfo = obj.Properties;
                foreach (var proper in this.GetType().GetProperties())
                {
                    var oldValue = proper.GetValue(old, null);
                    var newValue = proper.GetValue(this, null);
 
                    if (oldValue == null && newValue == null)
                    {
                        continue;
                    }
 
                    if (oldValue == null || !oldValue.Equals(newValue))
                    {
                        var mp3Att = proper.GetCustomAttributes(typeof(MediaPropertyAttribute), false).FirstOrDefault();
                        var shellProper = mediaInfo.GetProperty("System." + mp3Att);
                        Console.WriteLine(mp3Att);
                        SetPropertyValue(shellProper, newValue);
                    }
                }
            }
        }
 
        #region SetPropertyValue
        static void SetPropertyValue(IShellProperty prop, object value)
        {
            if (prop.ValueType == typeof(string[]))        //只读属性不会改变,故与实际类型不符的只有string[]这一种
            {
                string[] values = (value as string).Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                (prop as ShellProperty<string[]>).Value = values;
            }
            if (prop.ValueType == typeof(string))
            {
                (prop as ShellProperty<string>).Value = value as string;
            }
            else if (prop.ValueType == typeof(ushort?))
            {
                (prop as ShellProperty<ushort?>).Value = value as ushort?;
            }
            else if (prop.ValueType == typeof(short?))
            {
                (prop as ShellProperty<short?>).Value = value as short?;
            }
            else if (prop.ValueType == typeof(uint?))
            {
                (prop as ShellProperty<uint?>).Value = value as uint?;
            }
            else if (prop.ValueType == typeof(int?))
            {
                (prop as ShellProperty<int?>).Value = value as int?;
            }
            else if (prop.ValueType == typeof(ulong?))
            {
                (prop as ShellProperty<ulong?>).Value = value as ulong?;
            }
            else if (prop.ValueType == typeof(long?))
            {
                (prop as ShellProperty<long?>).Value = value as long?;
            }
            else if (prop.ValueType == typeof(DateTime?))
            {
                (prop as ShellProperty<DateTime?>).Value = value as DateTime?;
            }
            else if (prop.ValueType == typeof(double?))
            {
                (prop as ShellProperty<double?>).Value = value as double?;
            }
        }
        #endregion
 
        #region MediaPropertyAttribute
        [AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
        sealed class MediaPropertyAttribute : Attribute
        {
            public string PropertyKey { get; private set; }
            public MediaPropertyAttribute(string propertyKey)
            {
                this.PropertyKey = propertyKey;
            }
 
            public override string ToString()
            {
                return PropertyKey;
            }
        }
        #endregion
    }

 

 

剩下的就很简单了

读取当前文件夹下的所有mp3,批量替换标题

        static void Main(string[] args)
        {
            string fileDirectory = System.IO.Directory.GetCurrentDirectory();
            DirectoryInfo folder = new DirectoryInfo(fileDirectory);

            foreach (FileInfo file in folder.GetFiles("*.mp3"))
            {
                MediaTags mt = new MediaTags(file.FullName);
                mt.Title = file.FullName.Replace(fileDirectory + "\\", "").Replace(".mp3", "");
                mt.Commit(file.FullName);
            }
        }

 

完成效果,剩下批量转换就可以了

image




本文转自 sun8134 博客园博客,原文链接: http://www.cnblogs.com/sun8134/p/3838654.html  ,如需转载请自行联系原作者

相关文章
|
13天前
|
JSON 搜索推荐 API
抖音商品详情API接口:获取商品信息的指南
抖音商品详情API接口由抖音开放平台提供,允许第三方应用访问抖音小店的商品数据,包括基本信息、价格、库存及用户评价等。其优势在于数据实时性、自动化处理、市场分析及个性化推荐。通过注册账号、获取API密钥、阅读文档和构建请求,用户可高效获取商品信息,提升运营效率。未来,该接口将在电商领域发挥更大作用。
|
2月前
|
JSON API 开发工具
【Azure 应用服务】调用Azure REST API来获取 App Service的访问限制信息(Access Restrictions)以及修改
【Azure 应用服务】调用Azure REST API来获取 App Service的访问限制信息(Access Restrictions)以及修改
|
2月前
|
API Python
【Azure Developer】AAD API如何获取用户“Block sign in”信息(accountEnabled)
【Azure Developer】AAD API如何获取用户“Block sign in”信息(accountEnabled)
|
6天前
|
XML JSON API
淘宝商品详情API接口:获取商品信息的指南
淘宝详情API接口是淘宝开放平台提供的一种API接口,它允许开发者通过编程方式获取淘宝商品的详细信息。这些信息包括商品的基本属性、价格、库存状态、销售策略、卖家信息等,对于电商分析、市场研究或者商品信息管理等场景非常有用。
18 1
|
1月前
|
存储 API 数据库
如何使用 ef core 的 code first(fluent api)模式实现自定义类型转换器?
本文介绍了如何在 EF Core 的 Code First 模式下使用自定义类型转换器实现 JsonDocument 和 DateTime 类型到 SQLite 数据库的正确映射。通过自定义 ValueConverter,实现了数据类型的转换,并展示了完整的项目结构和代码实现,包括实体类定义、DbContext 配置、Repositories 仓储模式及数据库应用迁移(Migrations)操作。
50 6
如何使用 ef core 的 code first(fluent api)模式实现自定义类型转换器?
|
6天前
|
网络协议 API Windows
MASM32编程调用 API函数RtlIpv6AddressToString,windows 10 容易,Windows 7 折腾
MASM32编程调用 API函数RtlIpv6AddressToString,windows 10 容易,Windows 7 折腾
|
2月前
|
Java 应用服务中间件 开发工具
[App Service for Windows]通过 KUDU 查看 Tomcat 配置信息
[App Service for Windows]通过 KUDU 查看 Tomcat 配置信息
|
2月前
|
Java Windows
【Azure Developer】Windows中通过pslist命令查看到Java进程和线程信息,但为什么和代码中打印出来的进程号不一致呢?
【Azure Developer】Windows中通过pslist命令查看到Java进程和线程信息,但为什么和代码中打印出来的进程号不一致呢?
|
2月前
|
测试技术 API
【API管理 APIM】如何查看APIM中的Request与Response详细信息,如Header,Body中的参数内容
【API管理 APIM】如何查看APIM中的Request与Response详细信息,如Header,Body中的参数内容
|
2月前
|
JSON API 数据安全/隐私保护
从零开始认识 API,让网页信息成为你的「知识库」
本文介绍了API(应用程序编程接口)的概念及其在网络通信中的重要作用,并通过生动的例子解释了API的工作原理。API作为连接不同软件组件的桥梁,使得开发者能够构建出功能丰富且灵活的应用程序。文章进一步探讨了如何捕获API,包括查看官方文档、使用浏览器的F12工具观察网络请求,以及借助抓包工具捕获移动应用的API调用。通过这些技术,用户可以获取所需的API信息并加以利用。作为实例,文章展示了如何通过抓取知乎、少数派等平台的热门文章API,整合信息到个人博客或笔记系统中,创建个性化的信息中心。这一过程不仅提高了信息获取的效率,也为个性化内容消费开辟了新的途径。
下一篇
无影云桌面