iOS将AVAssetImageGenerator中的实际时间帧修剪为秒

时间:2014-08-01 16:59:51

标签: ios avassetimagegenerator

我试图扫描电影的每第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

如您所见,请求的时间是我想要的,但实际时间总是缩短为秒。如何获得第二部分的实际帧?或者也许我得到了正确的帧但是实际时间无效?

0 个答案:

没有答案