如何使用C#获取视频文件的持续时间?

时间:2010-03-15 09:00:12

标签: c# visual-studio visual-studio-2008 video

C#中允许我这样做的任何库吗?

3 个答案:

答案 0 :(得分:5)

http://johndyer.name/post/2005/07/22/Retreiving-the-duration-of-a-WMV-in-C.aspx

的google结果
using WMPLib; // this file is called Interop.WMPLib.dll

WindowsMediaPlayerClass wmp = new WindowsMediaPlayerClass();
IWMPMedia mediaInfo = wmp.newMedia("myfile.wmv"); 

// write duration
Console.WriteLine("Duration = " + mediaInfo.duration);

// write named attributes
for (int i=0; i<mediaInfo.attributeCount; i++)
{
Console.WriteLine(mediaInfo.getAttributeName(i) + " = " +  mediaInfo.getItemInfo(mediaInfo.getAttributeName(i)) );
}

答案 1 :(得分:0)

您可以尝试使用此扩展方法。

using Shell32;

public static class Extension
{
    public static string GetLength(this FileInfo info)
    {
        var shell = new ShellClass();
        var folder = shell.NameSpace(info.DirectoryName);
        var item = folder.ParseName(info.Name);

        return folder.GetDetailsOf(item, 27);
    }
}

答案 2 :(得分:0)

我希望以下代码段能为您提供帮助:

using WMPLib;
// ...your code here...

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

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