iOS getting actual timeframes in AVAssetImageGenerator trimmed to seconds

419 Views Asked by At

Im trying to scan every 10th frame of the movie as shown in example below:

-(void)processMovie {
    NSString* savedVideoPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];
    NSURL* url = [NSURL fileURLWithPath:savedVideoPath];

    AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:url options:nil];
    float secs = CMTimeGetSeconds([asset duration]);
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    generator.appliesPreferredTrackTransform=TRUE;
    [asset release];

    // building array of time with steps as 1/10th of second
    NSMutableArray* arr = [[[NSMutableArray alloc] init] retain];
    for(int i=0; i<secs*10; i++) {
        [arr addObject:[NSValue valueWithCMTime:CMTimeMake(i,10)]];
    }

    AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
        if (result == AVAssetImageGeneratorSucceeded) {
            float reqSec = CMTimeGetSeconds(requestedTime);
            float actSec = CMTimeGetSeconds(actualTime);
            NSLog(@"%f %f", reqSec, actSec);
        }
    };

    [generator generateCGImagesAsynchronouslyForTimes:arr completionHandler:handler];
}

Problem is my log shows following:

0.0 0.0
0.1 0.0
0.2 0.0
0.3 0.0
.......
3.0 3.0
3.1 3.0

As you see requested time is as I want it but actual time always trimmed to the second. How do I get actual frames for the fraction of the second? Or maybe Im getting correct frames but actualTime is invalid?

0

There are 0 best solutions below