C#获取从元数据中的视频文件时长 [英] C# Get video file duration from metadata
问题描述
我想从文件中读取元数据。我只需要视频 - >长度属性,但是我无法找到阅读本信息的简单方法。
I am trying to read metadata from a file. I only need the Video -> Length property, however I am unable to find a simple way of reading this information.
我想这将是相当容易的,因为它是可见默认情况下,在资源管理器,然而,这看起来是比我预期的方式更加复杂。我来用最接近的:
I figured this would be fairly easy since it is visible by default in Explorer, however this looks to be way more complicated than I anticipated. The closest I came was using:
Microsoft.DirectX.AudioVideoPlayback.Video video = new Microsoft.DirectX.AudioVideoPlayback.Video(str);
double duration = video.Duration;
然而,这将引发一个LoaderLock例外,我不知道该如何处理它。
However this throws a LoaderLock exception, and I don't know how to deal with it.
任何想法?
推荐答案
许多这样的细节由shell提供,这样你就可以通过向COM库微软壳牌控制和自动化(SHELL32)的引用,然后使用Folder.GetDetailsOf方法来查询扩展细节做到这一点。
Many of these details are provided by the shell, so you can do this by adding a reference to the COM Library "Microsoft Shell Controls and Automation" (Shell32), and then using the Folder.GetDetailsOf method to query the extended details.
我最近在寻找这一点,碰到的这个非常的MSDN C#通用论坛质疑。我结束了写这个作为一个扩展的方法对FileInfo:
I was recently looking for this and came across this very question on the MSDN C# General forums. I wound up writing this as an extension method to FileInfo:
public static Dictionary<string, string> GetDetails(this FileInfo fi)
{
Dictionary<string, string> ret = new Dictionary<string, string>();
Shell shl = new ShellClass();
Folder folder = shl.NameSpace(fi.DirectoryName);
FolderItem item = folder.ParseName(fi.Name);
for (int i = 0; i < 150; i++)
{
string dtlDesc = folder.GetDetailsOf(null, i);
string dtlVal = folder.GetDetailsOf(item, i);
if (dtlVal == null || dtlVal == "")
continue;
ret.Add(dtlDesc, dtlVal);
}
return ret;
}
如果你正在寻找特定的条目,你可以做类似的事情,但它会快得多,找出这些条目是在什么指数(长度为27指数我相信),只是查询这些信息。请注意,我没有做太多研究指数是否可以改变(我对此表示怀疑),这就是为什么我把字典方法。
If you're looking for specific entries, you can do something similar, though it will be far faster to find out what index those entries are at (Length is index 27 I believe) and just query those. Note, I didn't do much research into whether or not the index can change (I doubt it), which is why I took the dictionary approach.
这篇关于C#获取从元数据中的视频文件时长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!