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 byte开发者_运维百科s 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?
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.
Reading the file raw is the correct way of doing it.
For future users reading this post, you can find the Apple Quick Time File Format document that describes the .mov file format from the following links:
Classic Version of the QuickTime File Format Specification:
Actual PDF describing the Classic Version of the QuickTime File Format Specification
Current Version of the QuickTime File Format Specification
There is another good way to do this which will give you the option latter on to improve your application should you ever want to find the duration or other information for a quicktime .mov file plus other media containers: use the MediaInfo.dll you can reference it in your project and find the duration plus nearly all other aspects of the media file.
精彩评论