每当我尝试从我的视频资源生成静止帧时,它就会在0.000 ..秒时生成。我可以从我的日志消息中看到这一点。好消息是我可以在0.000 ..时获得图像,以显示在名为“myImageView”的UIImageView中。我认为问题是没有设置AVURLAssetPreferPreciseDurationAndTimingKey,但即使在我弄清楚如何做到这一点之后,它仍然无法正常运行..
这是我的......
time,actualTime和generate在Header中声明
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/videoTest4.m4v"]];
//UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
NSURL *url = [NSURL fileURLWithPath:path];
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:options];
Float64 durationSeconds = CMTimeGetSeconds([asset duration]);
generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
NSError *err = nil;
time = CMTimeMake(600,600.0);
CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:&actualTime error:&err];
UIImage *currentImg = [[UIImage alloc] initWithCGImage:imgRef];
myImageView.image = currentImg;
NSLog(@"The total Video Duration is %f",durationSeconds);
NSLog(@"The time I want my image is %f",CMTimeGetSeconds(time));
NSLog(@"The actual time my image was take was %f",CMTimeGetSeconds(actualTime));
我的控制台读取..
2011-04-28 18:49:59.062 videoTest [26553:207]总视频时长为1.880000
2011-04-28 18:49:59.064 videoTest [26553:207]我想要的图片时间是1.000000
2011-04-28 18:49:59.064 videoTest [26553:207]我的拍摄时间实际为0.000000
..........................
提前多谢你们..:)
答案 0 :(得分:22)
要解决此问题,您只需将 requestedTimeToleranceBefore 和 requestedTimeToleranceAfter 设置为kCMTimeZero for AVAssetImageGenerator。
答案 1 :(得分:5)
昨晚深夜,我有一个想法,确定今天早上有效。基本上我只是创建一个新的合成资产,然后创建一个时间范围,表示视频的一帧,每秒24帧。一旦我创建了这些组合,我就抓住每个组合的第一帧。我为每个帧执行此操作并创建一个包含所有帧的数组。这就是我做的..
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/videoTest4.m4v"]];
//UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
NSURL *url = [NSURL fileURLWithPath:path];
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:options];
Float64 durationFrames = CMTimeGetSeconds([asset duration]) * 24.0;
AVMutableComposition *myComp = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionVideoTrack = [myComp addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
NSError *error = nil;
BOOL ok = NO;
NSMutableArray* frameArray = [[NSMutableArray alloc] init];
generate = [[AVAssetImageGenerator alloc] initWithAsset:myComp];
NSError *err = nil;
for (int i = 0; i < floor(durationFrames); i++) {
CMTime startTime = CMTimeMake(i, 24);
CMTime endTime = CMTimeMake(i+1, 24);
CMTimeRange myRange = CMTimeRangeMake(startTime, endTime);
AVAssetTrack *sourceVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
ok = [compositionVideoTrack insertTimeRange:myRange ofTrack:sourceVideoTrack atTime:kCMTimeZero error:&error];
if (!ok) {
// Deal with the error.
}
time = CMTimeMake(0,1);
CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:&actualTime error:&err];
UIImage *currentImg = [[UIImage alloc] initWithCGImage:imgRef];
[frameArray addObject:currentImg];
[currentImg release];
}
NSLog(@"This video is calculated at %f Frames..",durationFrames);
NSLog(@"You made a total of %i Frames!!",[frameArray count]);
然后控制台读取..
2011-04-29 10:42:24.292 videoTest [29019:207]此视频以45.120000帧计算..
2011-04-29 10:42:24.293 videoTest [29019:207]你共制作了45帧!!