How do I get the duration of a video file using C#? [closed]

时光毁灭记忆、已成空白 提交于 2019-12-14 03:15:17

问题


Any library in C# that allows me to do that?


回答1:


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

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)) );
}



回答2:


You can try this Extension method.

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);
    }
}



回答3:


I hope following code snippet will help you :

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

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

and don't forget to add the reference of wmp.dll which will be present in System32 folder.



来源:https://stackoverflow.com/questions/2445939/how-do-i-get-the-duration-of-a-video-file-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!