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 ...
随机推荐
- 在VMware中创建一个新的虚拟机 ,安装Linux4.X系统 ,之后在此基础上安装openfiler(网络存储管理实用程序)
到此为止虚拟机的前期设置准备好了 下面来为此虚拟机添加iso镜像 (这个是在http://www.openfiler.com/community/download openfiler官网上面下载的) ...
- LoadRunner关联通用函数的学习
LoadRunner关联通用函数的学习 写这篇文章的时候,我先声明一下,本BLOG中的文章如果没有写出是转贴的一般就是本人原创. Loadrunner脚本中进行关联的时候,用到了一些函数,作用是把字符 ...
- java反射快速入门
笔记记在了掘金,发现掘金的markdown编辑器比博客园样式要好看不少 https://juejin.im/post/5d4e575af265da03e4674e9f
- 扩展gcd求逆元
当模数为素数时可以用费马小定理求逆元. 模数为合数时,费马小定理大部分情况下失效,此时,只有与模数互质的数才有逆元(满足费马小定理的合数叫伪素数,讨论这个问题就需要新开一个博客了). (对于一个数n, ...
- 二分查找总结及部分Lintcode题目分析 4
二分法不只能像之前的记录,可以找到index~第二种类型是找到二分答案.有以下几个例子,都是之前二分法的扩展,不再赘述,只记录下忽略的点,以后回顾多注意~ 1. wood cut class Solu ...
- t检验中的t值和p值是什么关系_t检验和p值的关系
t检验中的t值和p值是什么关系_t检验和p值的关系 t检验中通过样本均值 总体均值 样本标准差 样本量 可以计算出一个t值,这个t值和p值有什么关系? 根据界值表又会查出一个数,这个数和t值比较,得出 ...
- windows api(GDI)实现图片旋转
GDI实现图片旋转,博主在网上找了好多资料,都不太如意. 并且在尝试中发现,如果先用SetViewportOrgEx将HDC上的坐标原点移动到图片中心:再在HDC上的获取每个点,用三角函数进行变换,算 ...
- Wireless Password HDU - 2825
题意: 给出m个模式串,要求构造一长度为n的文本串,至少包括k种模式串,求有多少种可能的模式串. k<=10 然后可以想到状压 一个文本串,k种模式串,很容易想到AC自动机. 把所有的模式串放 ...
- Nodejs之路(四)—— MongoDB&MySQL
一.MongoDB 1.1概述 MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案.MongoDB 是一个介于关系数据库和非 ...
- 海量可视化日志分析平台之ELK搭建
ELK是什么? E=ElasticSearch ,一款基于的Lucene的分布式搜索引擎,我们熟悉的github,就是由ElastiSearch提供的搜索,据传已经有10TB+的数据量. L=LogS ...