我正在尝试以 60fps 的速度将整个视频转换为一系列图像,这意味着每秒视频生成 60 张图像...
为此,我正在使用 AVAssetImageGenerator 和 generateCGImagesAsynchronouslyForTimes 方法......
事情进展顺利,只是我在批处理执行时间方面遇到了严重的性能问题(13 秒视频大约需要 5 分钟)......
此外,在以下大小 CGSizeMake(512, 324) 之上,我遇到崩溃...
有没有人已经有过这种处理的经验并且知道如何减少执行时间以及能够以更高分辨率提取图像?
下面是我正在测试的代码...
NSURL *movieURL = [NSURL fileURLWithPath:getCaptureMoviePath()];
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:movieURL options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
generator.requestedTimeToleranceAfter=kCMTimeZero;
generator.requestedTimeToleranceBefore=kCMTimeZero;
NSMutableArray *thumbTimes=[NSMutableArray arrayWithCapacity:asset.duration.value];
for(int t=0;t < asset.duration.value;t=t+2) {
CMTime thumbTime = CMTimeMake(t, asset.duration.timescale);
NSLog(@"Time Scale : %d ", asset.duration.timescale);
NSValue *v=[NSValue valueWithCMTime:thumbTime];
[thumbTimes addObject:v];
}
NSLog(@"thumbTimes array contains %d objects : ", [thumbTimes count]);
[asset release];
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error) {
if (result != AVAssetImageGeneratorSucceeded) {
NSLog(@"couldn't generate thumbnail, error:%@", error);
} else {
NSLog(@"actual time: %lld/%d (requested: %lld/%d)",actualTime.value,actualTime.timescale,requestedTime.value,requestedTime.timescale);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyMMdd-HHmmss"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filename = [NSString stringWithFormat:@"%@.png", [formatter stringFromDate:[NSDate date]]];
NSString *filepath = [documentsDirectory stringByAppendingPathComponent:filename];
CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:filepath];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, NULL);
CGImageDestinationAddImage(destination, im, nil);
if (!CGImageDestinationFinalize(destination)) {
NSLog(@"Failed to write image to %@", filepath);
}
CFRelease(destination);
}
//[generator release];
};
CGSize maxSize = CGSizeMake(512, 324);
generator.maximumSize = maxSize;
[generator generateCGImagesAsynchronouslyForTimes:thumbTimes completionHandler:handler];
提前谢谢你,
j.