Search code examples
c#quicktimedurationclipmov

Detecting a MOV file's duration


I've got a client app written in C# and want to read the duration of a MOV file. I would have to create and host a Quicktime COM control in a Windows Form, then load the movie in it and get the duration. This isn't doable in my case as the client app is not a windows forms one.

Another way of doing it, which I have actually done, is to open the MOV file raw, examine the bytes until you're 12 bytes past the movie header atom of the clip ('mvhd') and get the timescale and duration from there. This hack I'm not comfortable with as it may not work if Apple changes the file format in the future.

I would prefer to avoid loading the MOV to a control as I'm only interested to get the duration of the clip and nothing else. Ideally I need to provide a string with the path of the mov file to the app and get the duration like that. Has anyone done it with unmanaged code?


Solution

  • First of all, there is no "built-in" way to deal with special file formats. You have to either parse the file yourself, or get some component to do it for you.

    In fact, parsing is not a hack, provided you code adheres to the documentation. You should get (in case of some companies, buy) the official format specification, and parse the file header according to the specification. The specs are obligatory, so you shouldn't expect the format to change for the same filetype.

    You can try to find a component which would do it for you. The library which contains a control to display the movie could also have some utility classes included. Of course the component will parse the header in exactly the same way as you would do it, so if the Quicktime format would ever change (which I personally doubt), all the components would fails the same way.