asp.net 获取mp3 播放时长
1 Shell32
//添加引用:COM组件的Microsoft Shell Controls And Automation
//然后引用 using Shell32;
//如果出现“无法嵌入互操作类型 请改用适用的接口”报错————修改引用里面的shell32属性嵌入互操作类型改为false
public static string GetMP3FileDurationAll(string fileName)
{
ShellClass sh = new ShellClass();
Folder dir = sh.NameSpace(Path.GetDirectoryName(fileName));
FolderItem item = dir.ParseName(Path.GetFileName(fileName));
string str = dir.GetDetailsOf(item, 27); // 获取歌曲时长。
FileOperate.Write_Txt(DateTime.Now.ToString("yyyyMMddHHmmss") + "_GetMP3", DateTime.Now + " \n\r" + str + "\n");
if (str != "")
{
var matchs = Regex.Match(str, @"(\d{2}):(\d{2}):(\d{2})");
var hour = Convert.ToInt32(matchs.Groups[1].Value);
var minute = Convert.ToInt32(matchs.Groups[2].Value);
var second = Convert.ToInt32(matchs.Groups[3].Value);
var len = hour * 3600 + minute * 60 + second + "|" + str;
return len;
}
else {
return "";
}
}
2.winmm.dll
[DllImport("Kernel32", CharSet = CharSet.Auto)]
static extern Int32 GetShortPathName(String path, StringBuilder shortPath, Int32 shortPathLength);
[DllImport("winmm.dll")]
public static extern int mciSendString(string m_strCmd, StringBuilder m_strReceive, int m_v1, int m_v2);
public static string MusicTime(string musicFilePath)//musicFilePath是歌曲文件地址
{
if (File.Exists(musicFilePath))//是否存在这个文件
{
//利用MCI命令,返回值为文件时间
StringBuilder shortpath = new StringBuilder(80);
GetShortPathName(musicFilePath, shortpath, shortpath.Capacity);
string musicname = shortpath.ToString();
StringBuilder buf = new StringBuilder(80);
mciSendString("close all", buf, buf.Capacity, 0);
mciSendString("open " + musicname + " alias media", buf, buf.Capacity, 0);
mciSendString("status media length", buf, buf.Capacity, 0);
return buf.ToString().Trim();
double ms = Convert.ToDouble(buf.ToString().Trim());
TimeSpan ts = new TimeSpan(0, 0, 0, 0, (int)ms);
return ts.ToString().Substring(3, 5);//这里你自己去决定返回的是什么格式
}
return "0";//如果文件不存在就返回"0"
}
3 ffmpeg
public static string Fromffmpeg(string fileName, string ffmpegVirtualPath)
{
string duration = "";
using (System.Diagnostics.Process pro = new System.Diagnostics.Process())
{
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.ErrorDialog = false;
pro.StartInfo.RedirectStandardError = true;
pro.StartInfo.FileName = System.Web.HttpContext.Current.Server.MapPath(ffmpegVirtualPath);// AppDomain.CurrentDomain.BaseDirectory +"ffmpeg.exe";
pro.StartInfo.Arguments = " -i " + fileName;
pro.Start();
System.IO.StreamReader errorreader = pro.StandardError;
pro.WaitForExit(1000);
string result = errorreader.ReadToEnd();
if (!string.IsNullOrEmpty(result))
{
result = result.Substring(result.IndexOf("Duration: ") +("Duration: ").Length, ("00:00:00").Length);
duration = result;
var matchs = Regex.Match(duration, @"(\d{2}):(\d{2}):(\d{2})");
var hour = Convert.ToInt32(matchs.Groups[1].Value);
var minute = Convert.ToInt32(matchs.Groups[2].Value);
var second = Convert.ToInt32(matchs.Groups[3].Value);
var len = hour * 3600 + minute * 60 + second + "|" + duration;
duration = len;
}
return duration;
}
}
浙公网安备 33010602011771号