1

我试图扫描电影的每 10 帧,如下例所示:

-(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];
}

问题是我的日志显示如下:

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

如您所见,请求的时间是我想要的,但实际时间总是修剪到秒。如何获得秒的一小部分的实际帧?或者我得到正确的帧但实际时间无效?

4

0 回答 0