c# - 如何从 mp4、wmv、flv、mov 视频中获取视频时长

标签 c# .net video mp4 duration

好的。实际上我主要需要 mp4 格式。但是,如果也有可能获得其他类型,那就太好了。我只需要读取文件的持续时间。我如何使用 C# 4.0 做到这一点?

所以我需要的是这个视频是这样的:13 分 12 秒

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

谢谢。

最佳答案

This answer关于 Shell32 的 P/Invoke 让我想起了 Windows API Code Pack访问常见的 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);
    }
}

关于c# - 如何从 mp4、wmv、flv、mov 视频中获取视频时长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10190906/

相关文章:

asp.net-mvc - 部署到 windows azure 后 html5 视频无法工作

c# - 使用任务并行库Task.WhenAny()时如何处理异常

c# - 学习单一游戏或 xna

c# - AutoCAD resbuf 迁移

c# - 为什么 ReSharper 想要对所有内容使用 'var'?

javascript - youtube 视频作为网站背景

c# - 在 C# 中生成 1 MB(或 n MB)文本文件

c# - 如何从 linq 表达式中获取字符串?

.net - CodeDom 和 Silverlight

c# - 尝试使用 ffmpeg 加入两个 mp4 视频文件但出现错误