MediaInfo
1: Create a C# project
2: Include into the project this (click me) file
3: Create a nice wrapper class to extract the values:
here is one for you;
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;
}
}
Stick that file in the same project.
4: Download Current version of MediaInfo: Download Page .Pick the verison that best suits you ( DLL only ). Make sure the .dll is in the executing directory of the application.
5: Usage:
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);
}