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 ...
随机推荐
- Laravel Illuminate\Http\Exceptions\PostTooLargeException
出错原因是: 请求的post的数据比 php.ini设定的 post_max_size大的原因 解决方法: 增加php.ini中 post_max_size和upload_max_filesize的设 ...
- Largest Submatrix
Largest Submatrix 给出一个\(n\times m\)的网格,网格里只放有字符a,b,c,d,w,x,,z,现在你可以将其中的w换成a,b,把x换成b,c,把y换成a,c,把z换成a, ...
- [转]Redis实现缓存,你应该懂的哪些思路!
场景一:类似于微博,实现关注和被关注功能. 思路: 对每个用户使用两个集合类型键,用来存储关注别人的用户和被该用户关注的用户.当用户A关注用户B的时候,执行两步操作: sadd user:A B sa ...
- 8-5接口测试用例设计与编写2 rest-assured
rest-assured 简约的接口测试DSL 支持xml json的结构化解析 支持xpath jsonpath gpath等多种解析方式 对Spring的支持比较前面 底层是httpclient ...
- python3和python2编码拾遗
py2编码 tr和unicode str和unicode都是basestring的子类.严格意义上说,str其实是字节串,它是unicode经过编码后的字节组成的序列.对UTF-8编码的str'苑'使 ...
- JVM中堆栈
1.JVM是基于堆栈的虚拟机.JVM为每个新创建的线程都分配一个堆栈.也就是说,对于一个Java程序来说,它的运行就是通过对堆栈的操作来完成的.堆栈以帧为单位保存线程的状态.JVM对堆栈只进行两种操作 ...
- Emacs基本操作说明
- 计算几何——直线交点poj1269
求直线交点还是要推一个公式的.. 见博客https://blog.csdn.net/u013050857/article/details/40923789 还要学一下向量的定点比分法 另外poj精度好 ...
- hdu多校第二场1009 (hdu6599) I Love Palindrome String 回文自动机/字符串hash
题意: 找出这样的回文子串的个数:它本身是一个回文串,它的前一半也是一个回文串 输出格式要求输出l个数字,分别代表长度为1~l的这样的回文串的个数 题解: (回文自动机和回文树是一个东西) 首先用回文 ...
- day 67 Django基础三之视图函数
Django基础三之视图函数 本节目录 一 Django的视图函数view 二 CBV和FBV 三 使用Mixin 四 给视图加装饰器 五 Request对象 六 Response对象 一 Dja ...