Hi all... building a macOS app that will allow users to gather data from certain areas of a quicktime movie. The user should be able to specify IN and OUT timecodes of what section of the video to examine, and the app should be able to seek to that area and do its work.
I thought I was on to the way of doing this by creating a CMTimeRange...
```
NSDictionary *options = @{AVURLAssetPreferPreciseDurationAndTimingKey: @YES};
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoFileURL options:options];
NSError *error;
AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:asset error:&error];
if (error) {
NSLog(@"Error:%@", error.localizedDescription);
}
AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVAssetReaderTrackOutput *trackOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:videoTrack outputSettings:nil];
[assetReader addOutput:trackOutput];
CMTime start = CMTimeMakeWithSeconds(_distanceFromVideoToStart, rate->fps); //this is the duration to be skipped
CMTime duration = CMTimeMakeWithSeconds(_distanceToBeScanned, rate->fps); //this is the duration to be examined.
CMTimeRange range = CMTimeRangeMake(start, duration);
[assetReader setTimeRange:range];
[assetReader startReading];
while ((sampleBuffer = [trackOutput copyNextSampleBuffer])) {
//gathering video data
} ```
This sort of works. My main problem is that if the user enters a "start" time 10 seconds into the video... a frame specific calculation needs to be made... if the video is 30fps... we need to skip ahead 300 frames, but at 24fps only 240. I thought that was what I was setting with the CMTime start = function... but perhaps I don't understand how this works? My values would be (for a 30fps video) 300 for start time and 30 fps... my understanding is that the output is A / B... so 300 / 30 is 10 seconds. That doesn't seem to work, I'm having to actually divide my first parameter by the frame rate (300 / 30, 30) in order for it to be close.
The other problem as far as I can tell, is that at the moment I'm being returned 24fps values... as if they are a default setting. So when I ask to seek 10 seconds in, it's really only seeking 8.08 seconds in, so the area being examined is incorrect.
So, first off... is this the best way to be seeking in a .mov? Next, how can I set the frame rate? And lastly... what am I not understanding about this CMTimeRange?
Thanks for taking the time to help me.