C#中获取mp4等媒体文件的播放时长

来源:互联网 发布:淘宝怎么看行业类目 编辑:程序博客网 时间:2024/05/21 23:59

添加一个类

public class FormatConverter    {        /// <summary>        /// 获取媒体文件播放时长        /// </summary>        /// <param name="path">媒体文件路径</param>        /// <returns></returns>        public static string GetMediaTimeLen(string path)        {            try            {                //ShellClass shell = new ShellClass();                  Shell32.Shell shell = new Shell32.ShellClass();                //文件路径                Shell32.Folder folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\")));                //文件名称                Shell32.FolderItem folderitem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 1));                return folder.GetDetailsOf(folderitem, 21);            }            catch (Exception ex)            {                return null;            }        }        public static string GetMediaTimes(string SongPath)        {            string dirName = Path.GetDirectoryName(SongPath);            string SongName = Path.GetFileName(SongPath);//获得文件名称              FileInfo fInfo = new FileInfo(SongPath);            ShellClass sh = new ShellClass();            Folder dir = sh.NameSpace(dirName);            FolderItem item = dir.ParseName(SongName);            return Regex.Match(dir.GetDetailsOf(item, -1), "\\d:\\d{2}:\\d{2}").Value;//获取文件时间           }      }

调用

string time = FormatConverter.GetMediaTimes(filefullname);