如何使用 C# 获取视频文件的时长?

发布于 2024-08-24 18:35:43 字数 1431 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

懵少女 2024-08-31 18:35:43

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

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)) );
}
你列表最软的妹 2024-08-31 18:35:43

您可以尝试这个扩展方法。

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

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);
    }
}
偏爱自由 2024-08-31 18:35:43

我希望以下代码片段对您有所帮助:

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文