好的。实际上我主要需要 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.dll
和 Microsoft.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/