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

论坛 期权论坛 脚本     
匿名网站用户   2020-12-21 10:32   11   0

添加一个类

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);
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP