媒体信息
1:创建一个C#项目
2:在项目中包含这个(点击我)文件
3:创建一个很好的包装类来提取值:
这是给你的;
public class MediaQuery
{
public Dictionary<string, string> QueryFile(string filePath)
{
MediaInfo mediaInfo = new MediaInfo();
string version = mediaInfo.Option("Info_Version");
if (!File.Exists(filePath))
throw new Exception("File Not Found");
try
{
mediaInfo.Option("Inform", "XML");
mediaInfo.Option("Complete", "1");
mediaInfo.Open(filePath);
string xml = mediaInfo.Inform();
IEnumerable<XElement> el = XElement.Parse(xml, LoadOptions.None).Elements();
XElement general = el.FirstOrDefault(e => e.Attribute("type").Value == "General");
XElement video = el.FirstOrDefault(e => e.Attribute("type").Value == "Video");
XElement audio = el.FirstOrDefault(e => e.Attribute("type").Value == "Audio");
Dictionary<string, string> values = new Dictionary<string, string>();
values = GetValues(general, "General");
values = GetValues(video, "Video", values);
values = GetValues(audio, "Audio", values);
return values;
}
catch (Exception ex)
{
throw new Exception("Problem Querying File", ex);
}
}
private Dictionary<string, string> GetValues(XElement xElement, string rootType)
{
Dictionary<string, string> values = new Dictionary<string, string>();
return GetValues(xElement, rootType, values);
}
private Dictionary<string, string> GetValues(XElement xElement, string rootType, Dictionary<string, string> values)
{
foreach (XElement element in xElement.Elements())
{
string key = rootType + "/" + element.Name.ToString();
if (!values.ContainsKey(key))
values.Add(key, element.Value.ToString());
else
{
int count = 1;
key = rootType + "/" + element.Name.ToString() + count.ToString();
while (values.ContainsKey(key))
{
count++;
key = rootType + "/" + element.Name.ToString() + count.ToString();
}
values.Add(key, element.Value.ToString());
}
}
return values;
}
}
将该文件粘贴在同一个项目中。
4:下载MediaInfo的当前版本:下载页面。选择最适合您的版本(仅限DLL)。确保 .dll 位于应用程序的执行目录中。
5:用法:
MediaQuery query = new MediaQuery();
Dictionary<string, string> results = query.QueryFile(@"C:\text.mov");
string videoBitRate;
if (results.TryGetValue("Video/Bit_rate", out videoBitRate) == false)
{
throw new Exception("Video bit rate not found");
}
else
{
// Do whatever you want with this....
Console.writeline("Video bitrate:" + videoBitRate);
}