If there are multiple media files in a folder like so:
MediaFiles(folder)
-> file1.mp4
-> file2.mp4
...
When we select all the files and
Right Click -> Properties
In the Properties windows on the Details Tab there is a Length field that shows the total runtime of the media files together like this:
Is it possible to get this info using C#?
As indicated in this link, with the help of the Microsoft.WindowsAPICodePack-Shell nuget package, you can get the total length as follows;
static void Main(string[] args)
{
DirectoryInfo dir = new DirectoryInfo(@"C:\to\your\path");
FileInfo[] files = dir.GetFiles("*.mp4");
var totalDuration = files.Sum(v => GetVideoDuration(v.FullName));
}
public static double GetVideoDuration(string filePath)
{
using (var shell = ShellObject.FromParsingName(filePath))
{
IShellProperty prop = shell.Properties.System.Media.Duration;
var t = (ulong)prop.ValueAsObject;
return TimeSpan.FromTicks((long)t).TotalMilliseconds;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With