asp.net 擷取mp3 播放時間長度

來源:互聯網
上載者:User

標籤: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 播放時間長度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.