Harun
Harun

Reputation: 5179

How to find duration of an uploaded mp4 or f4v video?

I've tried using directshowlib-2005 by installing k-lite mega codec pack. It can't find the duration of an mp4 file or f4v file (avi, wmv and flv has no problem). I use ImediaSeeking interface of directshowlib-2005 to find duration. But in case of mp4 and f4v the GetDuration method returns zero.

I know it is a codec problem, but I do not know which codec is to be installed to get duration of mp4 as well as f4v files.

The code I am using is shown below:

static public bool GetVideoLength(string fileName, out long length)
    {
        DirectShowLib.FilterGraph graphFilter = new DirectShowLib.FilterGraph();
        DirectShowLib.IGraphBuilder graphBuilder;
        //DirectShowLib.IMediaPosition mediaPos=null;
        DirectShowLib.IMediaSeeking mediaPos;
        length = 4294967296;

        try
        {
            graphBuilder = (DirectShowLib.IGraphBuilder)graphFilter;
            graphBuilder.RenderFile(fileName, null);
            //mediaPos = (DirectShowLib.IMediaPosition)graphBuilder;
            mediaPos = (DirectShowLib.IMediaSeeking)graphBuilder;                
           // mediaPos.get_Duration(out length);
            mediaPos.GetDuration(out length);

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {               
            mediaPos = null;
            graphBuilder = null;
            graphFilter = null;               

        }
    }

Can anyone please help me by telling me the exact codec which should be installed to find the duration as mentioned above?

Upvotes: 2

Views: 3158

Answers (1)

Shiv Kumar
Shiv Kumar

Reputation: 9799

I'd simply use MediaInfo. There is a CLI for it as well so you can call it from your code and get this info. It pretty much handles all kinds of codecs and containers.

Upvotes: 1

Related Questions