What's the best way to get video metadata from a video file in ASP.Net MVC using C#?
Have a look at MediaInfo
project (http://mediaarea.net/en/MediaInfo)
it gets extensive information about most media types, and the library is bundled with a c# helper class which is easy to use.
You can download the library and helper class for windows from here:
http://mediaarea.net/en/MediaInfo/Download/Windows (DLL without installer)
The helper class is located at Developers\Source\MediaInfoDLL\MediaInfoDLL.cs
, simply add it to your project and copy the MediaInfo.dll
to your bin.
Usage
you can obtain information by requesting specific parameter from the library, Here is a sample:
[STAThread]
static void Main(string[] Args)
{
var mi = new MediaInfo();
mi.Open(@"video path here");
var videoInfo = new VideoInfo(mi);
var audioInfo = new AudioInfo(mi);
mi.Close();
}
public class VideoInfo
{
public string Codec { get; private set; }
public int Width { get; private set; }
public int Heigth { get; private set; }
public double FrameRate { get; private set; }
public string FrameRateMode { get; private set; }
public string ScanType { get; private set; }
public TimeSpan Duration { get; private set; }
public int Bitrate { get; private set; }
public string AspectRatioMode { get; private set; }
public double AspectRatio { get; private set; }
public VideoInfo(MediaInfo mi)
{
Codec=mi.Get(StreamKind.Video, 0, "Format");
Width = int.Parse(mi.Get(StreamKind.Video, 0, "Width"));
Heigth = int.Parse(mi.Get(StreamKind.Video, 0, "Height"));
Duration = TimeSpan.FromMilliseconds(int.Parse(mi.Get(StreamKind.Video, 0, "Duration")));
Bitrate = int.Parse(mi.Get(StreamKind.Video, 0, "BitRate"));
AspectRatioMode = mi.Get(StreamKind.Video, 0, "AspectRatio/String"); //as formatted string
AspectRatio =double.Parse(mi.Get(StreamKind.Video, 0, "AspectRatio"));
FrameRate = double.Parse(mi.Get(StreamKind.Video, 0, "FrameRate"));
FrameRateMode = mi.Get(StreamKind.Video, 0, "FrameRate_Mode");
ScanType = mi.Get(StreamKind.Video, 0, "ScanType");
}
}
public class AudioInfo
{
public string Codec { get; private set; }
public string CompressionMode { get; private set; }
public string ChannelPositions { get; private set; }
public TimeSpan Duration { get; private set; }
public int Bitrate { get; private set; }
public string BitrateMode { get; private set; }
public int SamplingRate { get; private set; }
public AudioInfo(MediaInfo mi)
{
Codec = mi.Get(StreamKind.Audio, 0, "Format");
Duration = TimeSpan.FromMilliseconds(int.Parse(mi.Get(StreamKind.Audio, 0, "Duration")));
Bitrate = int.Parse(mi.Get(StreamKind.Audio, 0, "BitRate"));
BitrateMode = mi.Get(StreamKind.Audio, 0, "BitRate_Mode");
CompressionMode = mi.Get(StreamKind.Audio, 0, "Compression_Mode");
ChannelPositions = mi.Get(StreamKind.Audio, 0, "ChannelPositions");
SamplingRate = int.Parse(mi.Get(StreamKind.Audio, 0, "SamplingRate"));
}
}
You can easily obtain all information in string format by callingInform()
:
var mi = new MediaInfo();
mi.Open(@"video path here");
Console.WriteLine(mi.Inform());
mi.Close();
if you need more information about available parameters, you can simply query all of them by calling Options("Info_Parameters")
:
var mi = new MediaInfo();
Console.WriteLine(mi.Option("Info_Parameters"));
mi.Close();
It may be little late... You can do this with minimal code using the NuGet package of MediaToolKit
For more info get going from here MediaToolKit
I suggest you use ffmpeg with Process.Start, the code looks like follows:
private string GetVideoDuration(string ffmpegfile, string sourceFile) {
using (System.Diagnostics.Process ffmpeg = new System.Diagnostics.Process()) {
String duration; // soon will hold our video's duration in the form "HH:MM:SS.UU"
String result; // temp variable holding a string representation of our video's duration
StreamReader errorreader; // StringWriter to hold output from ffmpeg
// we want to execute the process without opening a shell
ffmpeg.StartInfo.UseShellExecute = false;
//ffmpeg.StartInfo.ErrorDialog = false;
ffmpeg.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
// redirect StandardError so we can parse it
// for some reason the output comes through over StandardError
ffmpeg.StartInfo.RedirectStandardError = true;
// set the file name of our process, including the full path
// (as well as quotes, as if you were calling it from the command-line)
ffmpeg.StartInfo.FileName = ffmpegfile;
// set the command-line arguments of our process, including full paths of any files
// (as well as quotes, as if you were passing these arguments on the command-line)
ffmpeg.StartInfo.Arguments = "-i " + sourceFile;
// start the process
ffmpeg.Start();
// now that the process is started, we can redirect output to the StreamReader we defined
errorreader = ffmpeg.StandardError;
// wait until ffmpeg comes back
ffmpeg.WaitForExit();
// read the output from ffmpeg, which for some reason is found in Process.StandardError
result = errorreader.ReadToEnd();
// a little convoluded, this string manipulation...
// working from the inside out, it:
// takes a substring of result, starting from the end of the "Duration: " label contained within,
// (execute "ffmpeg.exe -i somevideofile" on the command-line to verify for yourself that it is there)
// and going the full length of the timestamp
duration = result.Substring(result.IndexOf("Duration: ") + ("Duration: ").Length, ("00:00:00").Length);
return duration;
}
}
May it helps.