Getting MP4 File Duration with DirectShow

0

I need to get the duration of an mp4 file, preferably as a double in seconds. I was using DirectShow (see code below), but it keeps throwing a particularly unhelpful error. I'm wondering if someone has an easy solution to this. (Seriously, who knew that getting that information would be so difficult)

public static void getDuration(string moviePath)
    {
        FilgraphManager m_objFilterGraph = null;
        m_objFilterGraph = new FilgraphManager();
        m_objFilterGraph.RenderFile(moviePath);

        IMediaPosition m_objMediaPosition = null;
        m_objMediaPosition = m_objFilterGraph as IMediaPosition;

        Console.WriteLine(m_objMediaPosition.Duration);
    }

Whenever I run this code, I get the error: "Exception from HRESULT: 0x80040265"

I also tried using this: Getting length of video but it doesn't work either because I don't think that it works on MP4 files.

Seriously, I feel like there has to be a much easier way to do this.

Note: I would prefer to avoid using exe's like ffmpeg and then parsing the output to get the information.

c#
directshow
mp4
duration
directshow.net
asked on Stack Overflow Jul 5, 2012 by RedHack • edited May 23, 2017 by Community

3 Answers

2

You are approaching the problem correctly. You need to build a good pipeline starting from source .MP4 file and up to video and audio renderers. Then IMediaPosition.Duration will get you what you want. Currently you are getting VFW_E_UNSUPPORTED_STREAM because you cannot build the pipeline.

Note that there is no good support for MPEG-4 in DirectShow in clean Windows, you need a third party parser installed to add missing blocks. This is the likely cause of your problem. There are good Free DirectShow Mpeg-4 Filters available to fill this gap.

The code sample under the link Getting length of video is basically valid too, however it uses deprecated component which in additional make additional assumptions onto the media file in question. Provided that there is support for .MP4 in the system, IMediaPosition.Duration is to give you what you look for.

answered on Stack Overflow Jul 5, 2012 by Roman R. • edited May 23, 2017 by Community
0

You can use get_Duration() from IMediaPosition interface.

This return a double value with the video duration in seconds.

    Double Lenght;

    m_FilterGraph = new FilterGraph()

//Configure the FilterGraph()

    m_mediaPosition = m_FilterGraph as IMediaPosition;
    m_mediaPosition.get_Duration(out Length);
answered on Stack Overflow Jan 24, 2013 by JoaquinG
0

Using Windows Media Player Component also, we can get the duration of the video.
I hope that 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.

answered on Stack Overflow Nov 6, 2019 by Rish

User contributions licensed under CC BY-SA 3.0