开发者

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

开发者 https://www.devze.com 2022-12-23 02:34 出处:网络
Closed. This ques开发者_如何学编程tion is opinion-based. It is not currently accepting answers.
Closed. This ques开发者_如何学编程tion is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 4 years ago.

Improve this question

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


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消