4

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:

Properties windows

Is it possible to get this info using C#?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Codehelp
  • 4,157
  • 9
  • 59
  • 96
  • Possible duplicate of [How to get video duration from mp4, wmv, flv, mov videos](https://stackoverflow.com/questions/10190906/how-to-get-video-duration-from-mp4-wmv-flv-mov-videos) – TheGeneral Dec 21 '17 at 05:54
  • Possible duplicate of [How to get the Properties of a \*.mp3 File in C#](https://stackoverflow.com/questions/6505870/how-to-get-the-properties-of-a-mp3-file-in-c-sharp) – M.R.Safari Dec 21 '17 at 05:55

2 Answers2

8

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;
    }
}
ibubi
  • 2,469
  • 3
  • 29
  • 50
0

You can use windows media object and load the file and then get its properties. Read the folders file and loop through with each file and load them and read the property you want and may be store in DB.

Please refer from Microsoft site go here to check the more detail

Vijai Maurya
  • 101
  • 1
  • 1
  • 8
  • 1
    Pro Tip: an msdn link is ok but people don't want to go to another website that may or may not have the solution they've looking for, and if the link ever breaks and it's not on Wayback (often images are lost) then your answer is useless for future visitors. Consider [edit] your answer and highlight the specific parts that solve the question. See @ibubi answer, that's how we roll here on Stack Overflow. Good luck! – Jeremy Thompson Dec 21 '17 at 06:46
  • [When someone goes on Stack Overflow, the question "answer" should actually contain an answer. Not just a bunch of directions towards the answer.](https://meta.stackexchange.com/a/8259/171858) – Erik Philips Dec 21 '17 at 06:59
  • Ok, I was about to write the code but did not do... will take care while answering in future – Vijai Maurya Dec 21 '17 at 07:44