このメソッドを使用して、ビデオのフレームを生成します
-(NSMutableArray *)generateThumbnailsFromUrl:(NSURL *)videoURl{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURl options:nil];
Float64 durationFrames = CMTimeGetSeconds([asset duration]) * 30.0;
AVMutableComposition *myComp = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionVideoTrack = [myComp addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
NSError *error = nil;
BOOL ok = NO;
CGImageRef imgRef;
UIImage *currentImg;
CMTime time,startTime,endTime;
CMTimeRange myRange;
AVAssetTrack *sourceVideoTrack;
NSMutableArray* frameArray = [[NSMutableArray alloc] init];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:myComp];
@try {
for (int i = 0; i < floor(durationFrames); i++) {
startTime = CMTimeMake(i, 30);
endTime = CMTimeMake(i+1, 30);
myRange = CMTimeRangeMake(startTime, endTime);
sourceVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
ok = [compositionVideoTrack insertTimeRange:myRange ofTrack:sourceVideoTrack atTime:kCMTimeZero error:&error];
if (!ok) {
// Deal with the error.
NSLog(@"error %@ on %d",error,i);
}else{
time = CMTimeMake(0,1);
imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&error];
if(imgRef != nil)
{
currentImg = [[UIImage alloc] initWithCGImage:imgRef];
if(currentImg!=nil)
[frameArray addObject:currentImg];
}
}
currentImg=nil;
CGImageRelease(imgRef);
ok=NO;
}
}
@catch (NSException *exception) {
NSLog(@"exption is %@",exception);
}
return frameArray;}
しかし、私はこの行に深刻な問題を抱えていますimgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&error];
時々実行途中でここでフリーズしますが、例外なく結果も実行も継続もなし! 誰でも私を助けたり、問題を説明したりできますか?
コマンドの実行がより特定の時間であるかどうかを確認し、そのコマンドを強制終了する方法はありますか?