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 ...
随机推荐
- Django ORM 之 单表、多表查询
返回ORM目录 Django ORM Django ORM 之一 内容目录: 一.单表查询 二.多表查询 0.准备工作 一些说明: - 表myapp_person的名称是自动生成的,如果你要自定义表名 ...
- digitalpersona 开发
一 下载 sdk : https://codeload.github.com/iamonuwa/Digital-Persona-SDK/zip/master 二 解压后,安装SDK 三 找到安装目录( ...
- mysql 远程连接报错
./mysql -uzhu -p -h192.168.1.200 ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.200 ...
- arc098D Xor Sum 2
题意:给你一个数列,问有多少对(l,r)满足A[l]+A[l+1]+...+A[r]=A[l]^A[l+1]^...^A[r]? 标程: #include<bits/stdc++.h> u ...
- python 使用字符串
字符串方法 string.digits:包含数字0-9的字符串 string.letters:包含所有字母(大写或小写)的字符串 string.lowercase:包含所有小写字母的字符串 strin ...
- 【转载】浅谈Linux内存管理机制
经常遇到一些刚接触Linux的新手会问内存占用怎么那么多? 在Linux中经常发现空闲内存很少,似乎所有的内存都被系统占用了,表面感觉是内存不够用了,其实不然.这是Linux内存管理的一个优秀特性,在 ...
- myeclipse CTRL+1功能
有时候,在myeclipse或者eclipse中自动编译代码有错误,我们把鼠标放在错误一行能够自动显示出问题原因,但是有时显示问题让人有些匪夷所思,不知所云何物. 此时可以使用<ctrl> ...
- IntelliJ IDEA更换主题样式分享
原文地址:https://blog.csdn.net/liu865033503/article/details/79481785 .自定义主题样式网址:http://www.riaway.com/in ...
- POJ-2499-Binary Tree-思维题
Background Binary trees are a common data structure in computer science. In this problem we will loo ...
- Daemon 守护线程(27-11)
t2.setDaemon(True)不再等待里面的sleep(5). 当设成setDaemon(True)这个线程就不等了. 例子一: import threadingfrom time import ...