码迷,mamicode.com
首页 > Windows程序 > 详细

C#获取视频文件播放长度

时间:2017-06-16 15:10:51      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:stat   ram   soft   ring   count   ati   ros   indexof   microsoft   

下面两种方法只支持部分视频格式,一般格式mp3,wma等等支持

1.使用Shell32

添加引用,选择COM中的Microsoft Shell Controls And Automation引用

        /// <summary>
        /// 长度分钟(支持mp4?)
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetMediaTimeLenMinute(string path)
        {
            try
            {
                Shell32.Shell shell = new Shell32.Shell();
                //文件路径               
                Shell32.Folder folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\")));
                //文件名称             
                Shell32.FolderItem folderitem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 1));
                if (Environment.OSVersion.Version.Major >= 6)
                {
                    string mediaLength = folder.GetDetailsOf(folderitem, 27);
                    return mediaLength;
                }
                else
                {
                    string mediaLength = folder.GetDetailsOf(folderitem, 21);
                    return mediaLength;
                }
            }
            catch (Exception ex) { return null; }
        }
        /// <summary>
        /// 长度秒(支持mp4?)
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static int GetMediaTimeLenSecond(string path)
        {
            try
            {
                Shell32.Shell shell = new Shell32.Shell();
                //文件路径               
                Shell32.Folder folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\")));
                //文件名称             
                Shell32.FolderItem folderitem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 1));
                string len;
                if (Environment.OSVersion.Version.Major >= 6)
                {
                    len = folder.GetDetailsOf(folderitem, 27);
                }
                else
                {
                    len = folder.GetDetailsOf(folderitem, 21);
                }

                string[] str = len.Split(new char[] { ‘:‘ });
                int sum = 0;
                sum = int.Parse(str[0]) * 60 * 60 + int.Parse(str[1]) * 60 + int.Parse(str[2]);

                return sum;
            }
            catch (Exception ex) { return 0; }
        }

  

 

1.使用DirectShowLib.dll

可在NuGet中搜索到DirectShowLib添加引用

    public static string GetMediaTimeLen(string path)
    {
        var mediaDet = (IMediaDet)new MediaDet();
        DsError.ThrowExceptionForHR(mediaDet.put_Filename(path));// find the video stream in the fileint index;
        var type = Guid.Empty;
        for (int index = 0; index < 1000 && type != MediaType.Video; index++)
        {
            mediaDet.put_CurrentStream(index);
            mediaDet.get_StreamType(out type);
        }
        // retrieve some measurements from the video
        double frameRate;
        mediaDet.get_FrameRate(out frameRate);
        var mediaType = new AMMediaType();
        mediaDet.get_StreamMediaType(mediaType);
        var videoInfo = (VideoInfoHeader)Marshal.PtrToStructure(mediaType.formatPtr, typeof(VideoInfoHeader));
        DsUtils.FreeAMMediaType(mediaType);
        var width = videoInfo.BmiHeader.Width;
        var height = videoInfo.BmiHeader.Height;
        double mediaLength;//这个是视频长度,单位秒
        mediaDet.get_StreamLength(out mediaLength);
        var frameCount = (int)(frameRate * mediaLength);
        var duration = frameCount / frameRate;
        return "";
    }

  

 

C#获取视频文件播放长度

标签:stat   ram   soft   ring   count   ati   ros   indexof   microsoft   

原文地址:http://www.cnblogs.com/cuihongyu3503319/p/7027451.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!