C#获取MP3,WMA信息
用于获取MP3内部信息,包括歌曲名,歌手名等…… namespace FileBatchRemaer.domain
{
/// <summary>
/// Mp3信息结构
/// </summary>
public struct Mp3Info
{
public string identify; //TAG,三个字节
public string Title; //歌曲名,30个字节
public string Artist; //歌手名,30个字节
public string Album; //所属唱片,30个字节
public string Year; //年,4个字符
public string Comment; //注释,28个字节
public char reserved1; //保留位,一个字节
public char reserved2; //保留位,一个字节
public char reserved3; //保留位,一个字节
} /// <summary>
/// Mp3文件信息类
/// </summary>
public class Mp3FileInfo
{
Mp3Info info; /// <summary>
/// 构造函数,输入文件名即得到信息
/// </summary>
/// <param name="mp3FilePos"></param>
public Mp3FileInfo(String mp3FilePos)
{
info = getMp3Info(getLast128(mp3FilePos));
} /// <summary>
/// 获取整理后的Mp3文件名,这里以标题和艺术家名定文件名
/// </summary>
/// <returns></returns>
public String GetOriginalName()
{
return formatString(info.Title.Trim()) + "-" + formatString(info.Artist.Trim());
} /// <summary>
/// 去除\0字符
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private String formatString(String str)
{
return str.Replace("\0", "");
} /// <summary>
/// 获取MP3文件最后128个字节
/// </summary>
/// <param name="FileName">文件名</param>
/// <returns>返回字节数组</returns>
private byte[] getLast128(string FileName)
{ FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
Stream stream = fs;
stream.Seek(-, SeekOrigin.End); const int seekPos = ;
int rl = ;
byte[] Info = new byte[seekPos];
rl = stream.Read(Info, , seekPos); fs.Close();
stream.Close(); return Info;
} /// <summary>
/// 获取MP3歌曲的相关信息
/// </summary>
/// <param name = "Info">从MP3文件中截取的二进制信息</param>
/// <returns>返回一个Mp3Info结构</returns>
private Mp3Info getMp3Info(byte[] Info)
{ Mp3Info mp3Info = new Mp3Info(); string str = null;
int i;
int position = ;//循环的起始值
int currentIndex = ;//Info的当前索引值 //获取TAG标识 for (i = currentIndex; i < currentIndex + ; i++)
{
str = str + (char)Info[i];
position++;
} currentIndex = position;
mp3Info.identify = str; //获取歌名
str = null;
byte[] bytTitle = new byte[];//将歌名部分读到一个单独的数组中
int j = ;
for (i = currentIndex; i < currentIndex + ; i++)
{
bytTitle[j] = Info[i];
position++;
j++;
} currentIndex = position; mp3Info.Title = this.byteToString(bytTitle); //获取歌手名 str = null;
j = ;
byte[] bytArtist = new byte[];//将歌手名部分读到一个单独的数组中 for (i = currentIndex; i < currentIndex + ; i++)
{ bytArtist[j] = Info[i];
position++;
j++;
} currentIndex = position;
mp3Info.Artist = this.byteToString(bytArtist); //获取唱片名
str = null;
j = ;
byte[] bytAlbum = new byte[];//将唱片名部分读到一个单独的数组中 for (i = currentIndex; i < currentIndex + ; i++)
{ bytAlbum[j] = Info[i];
position++;
j++;
}
currentIndex = position;
mp3Info.Album = this.byteToString(bytAlbum); //获取年
str = null;
j = ;
byte[] bytYear = new byte[];//将年部分读到一个单独的数组中 for (i = currentIndex; i < currentIndex + ; i++)
{
bytYear[j] = Info[i];
position++;
j++;
} currentIndex = position;
mp3Info.Year = this.byteToString(bytYear); //获取注释 str = null;
j = ;
byte[] bytComment = new byte[];//将注释部分读到一个单独的数组中 for (i = currentIndex; i < currentIndex + ; i++)
{
bytComment[j] = Info[i];
position++;
j++;
} currentIndex = position;
mp3Info.Comment = this.byteToString(bytComment); //以下获取保留位
mp3Info.reserved1 = (char)Info[++position];
mp3Info.reserved2 = (char)Info[++position];
mp3Info.reserved3 = (char)Info[++position]; return mp3Info; } /// <summary>
/// 将字节数组转换成字符串
/// </summary>
/// <param name = "b">字节数组</param>
/// <returns>返回转换后的字符串</returns>
private string byteToString(byte[] b)
{ Encoding enc = Encoding.GetEncoding("GB2312");
string str = enc.GetString(b);
str = str.Substring(, str.IndexOf("#CONTENT#") >= ? str.IndexOf("#CONTENT#") : str.Length);//去掉无用字符
return str;
}
}
} wma:利用shell32获取信息:首先在项目中添加shell32.dll,在windows\system32\下面 1 public string[] getSongInfoFromWma(string FileName)
{
string[] wmaFileStruct = new string[];
MP3File mp3=new MP3File();
//create shell instance
Shell32.Shell shell = new Shell32.ShellClass();
//set the namespace to file path
Shell32.Folder folder = shell.NameSpace(FileName.Substring(,FileName.LastIndexOf("\\")));
//get ahandle to the file
Shell32.FolderItem folderItem = folder.ParseName(FileName.Substring(FileName.LastIndexOf("\\")+));
//did we get a handle ? wmaFileStruct[] = folder.GetDetailsOf(folderItem,); //歌曲名称
wmaFileStruct[] = folder.GetDetailsOf(folderItem,); //歌手名称
wmaFileStruct[] = folder.GetDetailsOf(folderItem,); //播放时间 return wmaFileStruct;
}
C#获取MP3,WMA信息的更多相关文章
- C# 获取 mp3文件信息
C# 获取 mp3文件信息[包括:文件大小.歌曲长度.歌手.专辑] 第一种方式:[代码已验证] // http://bbs.csdn.net/topics/390392612 string fil ...
- C# 获取 mp3文件信息【包括:文件大小、歌曲长度、歌手、专辑】
C# 获取 mp3文件信息[包括:文件大小.歌曲长度.歌手.专辑] 第一种方式:[代码已验证] // http://bbs.csdn.net/topics/390392612 string fil ...
- 【ASP.NET 进阶】获取MP3文件信息并显示专辑图片
突发奇想,想弄个显示MP3文件信息和专辑图片的小Demo,个人不是大牛,遂百度之,总算搞定,现分享如下. 效果图: GIF效果图: 主要是依靠2个DLL文件:ID3.dll 和 Interop.She ...
- PHP获取Mp3文件信息
扫描本地MP3文件,获取文件信息
- Python解密网易云音乐缓存文件获取MP3
前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:GeneralMonkey Python解密网易云音乐缓存文件获取MP3 ...
- 实现php获取mp3文件元信息如播放时间歌曲作者等
最近收集到一个php获取mp3文件元信息的类,感觉比较方便.现在分享给大家! 下面是使用方式和测试方式: <?php include_once 'mp3file.class.php'; func ...
- 【Python】 获取MP3信息replica
replica 初衷是想要整理iphone中的音乐.IOS(我自己的手机还是IOS8.3,新版本的系统可能有变化了)自带的音乐软件中所有音乐文件都存放在/var/mobile/Media/iTunes ...
- Python实例获取mp3文件的tag信息
下面利用一个python的实例程序,来学习python.这个程序的目的就是分析出所有MP3文件的Tag信息并输出. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- Mp3tag(MP3文件信息修改器) V2.79a 多语绿色版
软件名称: Mp3tag(MP3文件信息修改器) 软件语言: 多国语言 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 3.0MB 图片预览: 软件简介: Mp3Tag 是一款m ...
随机推荐
- Java中怎样实现字符串截取
使用substring()对字符串进行截取: /** * str.indexOf()查找下标 * substring();//字符串截取 * length();//字符串长度 * */ @Test p ...
- leetcood学习笔记-45-跳跃游戏二
题目描述: 第一次提交;超时 class Solution: def jump(self, nums: List[int]) -> int: l = [] for i in range(len( ...
- 【JZOJ6353】给(ca)
description analysis 很妙的\(DP\) 设\(f[i][j]\)表示已经放了\(i\)个叶子节点.根到当前节点走了\(j\)步向左的方案数 考虑调整\(DP\)方式,钦定伸出左儿 ...
- day22_4-pickle模块
# 参考资料:# python模块(转自Yuan先生) - 狂奔__蜗牛 - 博客园# https://www.cnblogs.com/guojintao/articles/9070485.html ...
- Django框架中session存储到redis中的配置
本文链接:https://blog.csdn.net/linqunbin/article/details/94786313————————————————版权声明:本文为CSDN博主「linqunbi ...
- [课后作业] 第002讲:用Python设计第一个游戏 | 课后测试题
试题: 0. 什么是BIF? 1. 用课堂上小甲鱼教的方法数一数 Python3 提供了多少个 BIF? 2. 在 Python 看来:'FishC' 和 'fishc' 一样吗? 3. 在小甲鱼看来 ...
- Sequence POJ - 3581 后缀数组
题意: 将一个序列分成非空的三部分,将每部分翻转后组合成一个新的序列, 输出这样操作得到的序列中字典序最小的序列 (保证第一个数是数组中最大的元素) 题解: 把数组当作串串. 因为第一个数最大,所以我 ...
- 基于Java Properties类设置本地配置文件
一.Java Properties类介绍 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件, ...
- (转)常用的三种修改mysql最大连接数的方法
MYSQL数据库安装完成后,默认最大连接数是100,一般流量稍微大一点的论坛或网站这个连接数是远远不够的,增加默认MYSQL连接数的方法有两个 方法一:进入MYSQL安装目录 打开MYSQL配置文件 ...
- CodeForces 258D Little Elephant and Broken Sorting(期望)
CF258D Little Elephant and Broken Sorting 题意 题意翻译 有一个\(1\sim n\)的排列,会进行\(m\)次操作,操作为交换\(a,b\).每次操作都有\ ...