Getting length of video

26,988

Solution 1

Here is an example:

using DirectShowLib;
using DirectShowLib.DES;
using System.Runtime.InteropServices;

...

var mediaDet = (IMediaDet)new MediaDet();
DsError.ThrowExceptionForHR(mediaDet.put_Filename(FileName));

// find the video stream in the file
int index;
var type = Guid.Empty;
for (index = 0; index < 1000 && type != MediaType.Video; index++)
{
    mediaDet.put_CurrentStream(index);
    mediaDet.get_StreamType(out type);
}

// retrieve some measurements from the video
double frameRate;
mediaDet.get_FrameRate(out frameRate);

var mediaType = new AMMediaType();
mediaDet.get_StreamMediaType(mediaType);
var videoInfo = (VideoInfoHeader)Marshal.PtrToStructure(mediaType.formatPtr, typeof(VideoInfoHeader));
DsUtils.FreeAMMediaType(mediaType);
var width = videoInfo.BmiHeader.Width;
var height = videoInfo.BmiHeader.Height;

double mediaLength;
mediaDet.get_StreamLength(out mediaLength);
var frameCount = (int)(frameRate * mediaLength);
var duration = frameCount / frameRate;

Solution 2

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<int, int, long> 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<int, int, long>(size[0], size[1], inputFile.Metadata.Duration.Ticks);
}

Solution 3

The open-source tool MediaInfo provides comprehensive meta-data for media files and can be used easily from your own application in DLL form:

void* Hande=MediaInfo::OpenQuick("**FILENAME**", "**VERSION**;**APP_NAME**;**APP_VERSION**")
MediaInfo::Inform()

Solution 4

I have tried to get the video length in a bit different way :
Actually using Windows Media Player Component also, we can get the duration of the video.
Following code snippet may help you guys :

using WMPLib;
// ...
var player = new WindowsMediaPlayer();
var clip = player.newMedia(filePath);
Console.WriteLine(TimeSpan.FromSeconds(clip.duration));

and don't forget to add the reference of wmp.dll which will be present in System32 folder.

Solution 5

you can get all sorts of information about many types of video formats including their duration with ffmpeg by using the -i flag:

ffmpeg -i videofile.whatever

If you want a nice library that can wrap ffmpef for you in C# then you can use MediaHandlerPro

Share:
26,988
MZimmerman6
Author by

MZimmerman6

I graduated from Drexel University in June 2013 with my Master's of Science in electrical engineering. While attending school my main focus was on digital signal process for audio, primarily music. Most of the work I have done over the years has been very much focused on real-time audio processing and feature extraction on mobile devices, namely iDevices. I have also been getting more and more into machine learning and pattern recognition algorithms. I do some freelance work on the side as well. Please feel free to contact me if you would like to know more.

Updated on December 17, 2020

Comments

  • MZimmerman6
    MZimmerman6 over 3 years

    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 say how. I have downloaded ffmpeg, but have no clue how to actually use it and there does not seem to be any example of how to use it to get the video duration. I see how you can use it to convert videos, but I simply just want to know the duration of a video. All of the other information does not matter.

    Is there any way of doing this simply, whether it be in C#, python, java, whatever, that will just return a string that indicates the length of a video file.

    Please provide examples if possible. Thanks in advance!

    Assume standard file formats, such as wmv, avi, mp4, mpeg. Stuff that has metadata.