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.

Community
  • 1
  • 1
RedHack
  • 285
  • 7
  • 18
  • After some exploration, it looks like I cannot actually get that information with DirectShow because it doesn't support MP4, which is mind-boggling. In any case, any help would be appreciated. – RedHack Jul 05 '12 at 14:14

3 Answers3

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.

Community
  • 1
  • 1
Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • Thanks! But can you link me to any sample code on how to get the GDCL Mpeg-4 filters working with DirectShow, I couldn't find any info on the site about how to do this. – RedHack Jul 05 '12 at 14:26
  • You basically need to download those DLLs and `regvsr32` them. Then your code snippet should be good it its current shape. Another possible reason is that MP4 file contains streams that cannot be decoded, e.g. something rare/non-standard or no matching deocder like H.264 is currently installed. You won't be able to get duration using this simple method in your original post, you will need to do it in a slightly "fine tuned" way. – Roman R. Jul 05 '12 at 14:27
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);
JoaquinG
  • 2,060
  • 1
  • 15
  • 20
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.

Rish
  • 1,303
  • 9
  • 22