标签: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 ""; }
标签:stat ram soft ring count ati ros indexof microsoft
原文地址:http://www.cnblogs.com/cuihongyu3503319/p/7027451.html