I am having trouble finding a simple example of how to get the video length of a file programmatically. Many people say, oh use this library/wrapper or whatever, but do not
The easist and flawless solution I found is to use MediaToolkit nuget package.
using MediaToolkit;
// a method to get Width, Height, and Duration in Ticks for video.
public static Tuple GetVideoInfo(string fileName)
{
var inputFile = new MediaToolkit.Model.MediaFile { Filename = fileName };
using (var engine = new Engine())
{
engine.GetMetadata(inputFile);
}
// FrameSize is returned as '1280x768' string.
var size = inputFile.Metadata.VideoData.FrameSize.Split(new[] { 'x' }).Select(o => int.Parse(o)).ToArray();
return new Tuple(size[0], size[1], inputFile.Metadata.Duration.Ticks);
}