views:

85

answers:

2

All QTKit examples use seconds for making ranges. I, unfortunately, have frame numbers and need to be frame accurate. I suppose I could multiply up by the frame rate if I could figure out how to get that out of my movie.

+1  A: 

You should be able to calculate the frame rate of a given video media by querying the following mediaAttributes of a QTMedia:

  • QTMediaDurationAttribute

  • QTMediaSampleCountAttribute

(they are described in the QTKit docs here)

and use the following formula for calculation:

QTTime    duration     = ... // value get from mediaAttribute 
NSNumber  sample_count = ... // value get from mediaAttribute
double    fps = (sample_count.longValue * duration.timeScale) / duration.timeValue;

Disclaimer:

Note that I have not tried if this works, but that it is how I expect it to work based on my experience the QuickTime C APIs and the QuickTime File Format.

Good Luck!

Bjoern
+1  A: 

Multiplying by the frame rate is not frame accurate because many of the file containers and codecs that Quicktime uses make use of variable frame rates to get better compression. You'll notice this in any kind of movie that has freezes frame for any length of time. See macbreak's The Road to 1080p, part1 as an example.

You can do frame accurate ranges with the QTMovie methods frameStartTime:atTime and frameEndTime:atTime introduced in OSX 10.6. These will give you the start and end of a frame respectively without doing frame decoding.

For example to count all the frames in a movie:

// Initialize QTMovie object called 'movie', disable looping, etc

[movie gotoEnd];
QTTime endTime = [movie currentTime];

[movie gotoBeginning];
QTTime curTime = [movie currentTime];

unsigned long numFrames = 0;
while (true)
{
    % get the end time of the current frame  
    [movie frameEndTime:&curTime];

    numFrames++;

    % If we get to the last frame, stop counting
    if (QTTimeCompare(curTime, endTime) == NSOrderedSame)
    {
       break;
    }
}
Nick Haddad
D'uh voted too early: I cannot find any reference to `frame(Start|End)Time:atTime:` in either the headers or the QTMovie Class Reference. Google only points to this post AND a category on QTMovie hosted on Google code which defines such methods. Your objection should stand, though: It's not safe to simply multiply the movie's framerate with a time in order to achieve frame accuracy.
danyowdee