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 ...
随机推荐
- Android开发 retrofit下载与上传
前言 此博客只讲解retrofit下载与上传的使用,其实与其说是retrofit的下载与上传还不如说,依然是Okhttp的下载与上传.如果你需要了解retrofit入门请查看这篇博客(此博客不在详细讲 ...
- iptables 命令大全
1.连续端口配置 iptables可以方便的配置多个端口.其中根据端口的连续性,又可分为连续端口配置和不连续端口配置. 如: -A INPUT -p tcp –dport 21:25 -j DROP/ ...
- thinkphp 获取模板地址
为了更方便的输出模板文件,新版封装了一个T函数用于生成模板文件名. 用法: 大理石平台检验标准 T([资源://][模块@][主题/][控制器/]操作,[视图分层]) T函数的返回值是一个完整的模板文 ...
- python实现百度OCR图片识别
一.直接上代码 import base64 import requests class CodeDemo: def __init__(self,AK,SK,code_url,img_path): se ...
- js 当前时间和对比时间的比较
<!DOCTYPE><html> <head> <meta charset="utf-8" /> <title>功能:当 ...
- ajax跨域获取网站json数据
由于自己的公司的项目需要调用视频地址 1:当为链接时:直接在播放器用数据库查找的地址 2:当为外部链接时:直接用window.location.href('数据库查找的地址') 3:当为H5链接时:使 ...
- iframe 的那些事儿
项目中有不少地方用到iframe,今儿把使用iframe遇到的一些问题一块儿总结一下. 1.javascript监听iframe加载完成事件 iframe加载过程需要一定时间,这个加载过程常常出现白屏 ...
- 2day:Python基础
基础知识: 1.python文件的后缀名:.py 2 .Windows Python的执行方式:Python 解释器路径 py文件路径 例:C:\python3\python.exe d:\1.py ...
- Facebook分布式框架—Thrift介绍。
Thrift介绍 Thrift是一个分布式RPC框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, ...
- 04_Mybatis输入\出映射
1. 输入映射 通过paramterType指定输入参数的类型,类型可以是简单类型.hashmap.pojo的包装类. 1.1 传递pojo的包装对象 1.需求 完成用户信息的综合查询,需要传 ...