BenC
BenC

Reputation: 31

Determine actual frame rate of a stream using QTMovie

I am using QTMovie with QTMovieOpenForPlaybackAttribute:YES, and using a QTMovieView to display it. I need to calculate the framerate it is achieving.

One way I can think of doing this is to have a callback which is called every time a frame is about to display or is ready to be displayed - is anyone familiar with such a callback?

Another way would be to have a timer which uses -currentFrameImage and compares it with the last frame image it tested - however firstly I don't know how you would go about comparing two NSImages, and secondly I would imagine this would be problematic if two sequential frames were the same, it would effectively assume a frame was dropped when it was not

The last way I can think of would be to again use a timer, this time to call -currentTime. I have tried this, however, for some reason, the timeScale is set to 1000000000. I read that the time scale is supposed to be 100*fps, so, why is currentTime returning that the FPS is 10000000? This seems completely incorrect. There are no flags set in the QTTime returned.

I have searched everywhere for information on this - any searches to do with frame rate only lead me to how to set a frame rate on capture which is not what I am looking for.

Upvotes: 3

Views: 1036

Answers (1)

Davyd Geyl
Davyd Geyl

Reputation: 4623

Try this:

- (double)frameRate
{
    double result = 0;

    for (QTTrack* track in [_movie tracks])
    {
        QTMedia* trackMedia = [track media];

        if ([trackMedia hasCharacteristic:QTMediaCharacteristicHasVideoFrameRate])
        {
            QTTime mediaDuration = [(NSValue*)[trackMedia attributeForKey:QTMediaDurationAttribute] QTTimeValue];
            long long mediaDurationScaleValue = mediaDuration.timeScale;
            long mediaDurationTimeValue = mediaDuration.timeValue;
            long mediaSampleCount = [(NSNumber*)[trackMedia attributeForKey:QTMediaSampleCountAttribute] longValue];
            result = (double)mediaSampleCount * ((double)mediaDurationScaleValue / (double)mediaDurationTimeValue);
            break;
        }
    }
    return result;
}

Upvotes: 3

Related Questions