如何从mp4,wmv,flv,movvideo获取video时长

好的。 其实我主要需要mp4格式。 但是,如果有可能获得其他类型,那将是不错的。 我只需要读取文件的持续时间。 我怎么能用C#4.0做到这一点?

所以我需要的是这个video就像: 13 minutes 12 seconds

我也可以使用3个第三方的前任。 就像他们将有关文件的信息保存到文本文件中一样。 我可以解析该文本文件。

谢谢。

您可以通过DirectShow.NET包装库使用DirectShow API MediaDet对象。 请参阅获取代码示例的video长度 , get_StreamLength以秒为单位获取持续时间。 这假设Windows安装了MPEG-4解复用器(需要第三方组件与Windows之前的7,我相信这同样适用于cezor的另一个答案 ,尽管可以自由地重新分配组件)。

您也可以使用Windows Media Player,但它不支持您请求的alle文件类型

 using WMPLib; public Double Duration(String file) { WindowsMediaPlayer wmp = new WindowsMediaPlayerClass(); IWMPMedia mediainfo = wmp.newMedia(file); return mediainfo.duration; } } 

关于Shell32的P / Invoke的答案提醒我使用Windows API代码包来访问常见的Windows Vista / 7/2008 / 2008R2 API。

使用包含的示例中的PropertyEdit演示,可以非常轻松地找出Shell32 API以获取各种媒体文件属性,例如持续时间。

我假设同样的先决条件适用于安装正确的解复用器,但它非常简单,因为它只需要添加对Microsoft.WindowsAPICodePack.dllMicrosoft.WindowsAPICodePack.Shell.dll引用以及以下代码:

 using Microsoft.WindowsAPICodePack.Shell; using Microsoft.WindowsAPICodePack.Shell.PropertySystem; using (ShellObject shell = ShellObject.FromParsingName(filePath)) { // alternatively: shell.Properties.GetProperty("System.Media.Duration"); IShellProperty prop = shell.Properties.System.Media.Duration; // Duration will be formatted as 00:44:08 string duration = prop.FormatForDisplay(PropertyDescriptionFormatOptions.None); } 

其他的东西

MPEG-4 / AAC音频媒体文件的一些常见属性:

 System.Audio.Format = {00001610-0000-0010-8000-00AA00389B71} System.Media.Duration = 00:44:08 System.Audio.EncodingBitrate = ?56kbps System.Audio.SampleRate = ?32 kHz System.Audio.SampleSize = ?16 bit System.Audio.ChannelCount = 2 (stereo) System.Audio.StreamNumber = 1 System.DRM.IsProtected = No System.KindText = Music System.Kind = Music 

如果您正在寻找可用的元数据,则可以轻松遍历所有属性:

 using (ShellPropertyCollection properties = new ShellPropertyCollection(filePath)) { foreach (IShellProperty prop in properties) { string value = (prop.ValueAsObject == null) ? "" : prop.FormatForDisplay(PropertyDescriptionFormatOptions.None); Console.WriteLine("{0} = {1}", prop.CanonicalName, value); } } 

恕我直言,您可以使用MediaInfo ,它可以为您提供有关媒体文件的大量信息。
它有一个CLI,因此您可以从代码中使用它并获取所需的信息。
你可以看一下这个链接 。

我想你正在寻找FFMPEG – http://www.ffmpeg-csharp.com/

还有一些免费的替代品你可以在这个问题中阅读它们 – 在.net中使用FFmpeg?

  FFMpeg.NET FFMpeg-Sharp FFLib.NET 

您可以在此链接中查看使用FFMPEG并查找持续时间的示例 – http://jasonjano.wordpress.com/2010/02/09/a-simple-c-wrapper-for-ffmpeg/

  public VideoFile GetVideoInfo(string inputPath) { VideoFile vf = null; try { vf = new VideoFile(inputPath); } catch (Exception ex) { throw ex; } GetVideoInfo(vf); return vf; } public void GetVideoInfo(VideoFile input) { //set up the parameters for video info string Params = string.Format("-i {0}", input.Path); string output = RunProcess(Params); input.RawInfo = output; //get duration Regex re = new Regex("[D|d]uration:.((\\d|:|\\.)*)"); Match m = re.Match(input.RawInfo); if (m.Success) { string duration = m.Groups[1].Value; string[] timepieces = duration.Split(new char[] { ':', '.' }); if (timepieces.Length == 4) { input.Duration = new TimeSpan(0, Convert.ToInt16(timepieces[0]), Convert.ToInt16(timepieces[1]), Convert.ToInt16(timepieces[2]), Convert.ToInt16(timepieces[3])); } } } 

FFMPEG项目有一个名为ffprobe的工具,它可以为您提供有关多媒体文件的信息,并以精确格式化的JSON输出信息。

看一下这个答案的例子。

同时使用Windows Media Player组件,我们可以获得video的持续时间。
以下代码段可能会帮助您:

 using WMPLib; // ... var player = new WindowsMediaPlayer(); var clip = player.newMedia(filePath); Console.WriteLine(TimeSpan.FromSeconds(clip.duration)); 

并且不要忘记添加将存在于System32文件夹中的wmp.dll的引用。

我发现NReco.VideoInfo库是最好的选择,比上面的一些简单得多。 这是一个简单的给库提供文件路径并吐出元数据:

 var ffProbe = new FFProbe(); var videoInfo = ffProbe.GetMediaInfo(blob.Uri.AbsoluteUri); return videoInfo.Duration.TotalMilliseconds; 
 StreamReader errorreader; string InterviewID = txtToolsInterviewID.Text; Process ffmpeg = new Process(); ffmpeg.StartInfo.UseShellExecute = false; ffmpeg.StartInfo.ErrorDialog = false; ffmpeg.StartInfo.RedirectStandardError = true; ffmpeg.StartInfo.FileName = Server.MapPath("ffmpeg.exe"); ffmpeg.StartInfo.Arguments = "-i " + Server.MapPath("videos") + "\\226.flv"; ffmpeg.Start(); errorreader = ffmpeg.StandardError; ffmpeg.WaitForExit(); string result = errorreader.ReadToEnd(); string duration = result.Substring(result.IndexOf("Duration: ") + ("Duration: ").Length, ("00:00:00.00").Length);