1

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?

Andrei V
  • 1,468
  • 3
  • 17
  • 32
  • Look into CMTimeMake, you can define all you need with that , http://stackoverflow.com/questions/4001755/trying-to-understand-cmtime-and-cmtimemake – Daniel Aug 01 '14 at 17:51
  • @Daniel as you see in my code I do call CMTimeMake and requestedTime in handle function does show that it is correct. Its the actualTime that is given to me always trimmed – Andrei V Aug 01 '14 at 18:13
  • 1
    Found my problem: I needed to set generator.requestedTimeToleranceBefore = generator.requestedTimeToleranceAfter = kCMTimeZero; – Andrei V Aug 01 '14 at 18:51

0 Answers0