5
我想要整個一個視頻轉換成圖像序列在60fps的速度,這意味着每秒視頻產生的60張......的iOS AVFoundation - 以60fps的視頻轉換成圖像
要這樣做,我正在利用AVAssetImageGenerator和generateCGImagesAsynchronouslyForTimes方法...
事情走得很好,除了我有嚴重的性能問題regarging批處理執行時間(大約5分鐘的13秒視頻)。 ..
此外,以上大小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];
請多關照,
學家
我遇到了同樣的問題,除了我的情況,我試圖切割大約14個非常短的視頻剪輯。你有沒有想過任何可能的優化? – afrederick