標籤:lsof virtual proc 屬性 use div cfile arguments direct
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; } }
asp.net 擷取mp3 播放時間長度