我无法从1.8秒的视频中获得用于生成静止图像的精确CMTime [英] I cannot get a precise CMTime for Generating Still Image from 1.8 second Video
问题描述
每次我尝试从视频资产生成静止帧时,都会在0.000 ..秒的时间生成该静止帧.我可以从我的日志消息中看到这一点.好消息是,我可以在0.000 ..的时间获取图像,以显示在名为"myImageView"的UIImageView中.我以为问题是未设置AVURLAssetPreferPreciseDurationAndTimingKey,但是即使在我知道如何做到这一点之后,它仍然无法正常工作.
Every time I try to generate a still frame from my video asset, it is generated at a time of 0.000.. seconds. I can see this from my log message. The good thing is that I can get the image at time 0.000.. to show up in a UIImageView called "myImageView." I thought the problem was that AVURLAssetPreferPreciseDurationAndTimingKey was not set, but even after I figured out how to do that, it still does not function..
这就是我所拥有的..
Here is what I have..
时间,实际时间和生成在标头中声明
time, actualTime, and generate are declared in the 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.062 videoTest[26553:207] The total Video Duration is 1.880000
2011-04-28 18:49:59.064 videoTest [26553:207]我想要我的图像的时间是1.000000
2011-04-28 18:49:59.064 videoTest[26553:207] The time I want my image is 1.000000
2011-04-28 18:49:59.064 videoTest [26553:207]我的图像实际拍摄时间为0.000000
2011-04-28 18:49:59.064 videoTest[26553:207] The actual time my image was take was 0.000000
..........................
..........................
非常感谢你们..:)
推荐答案
要解决此问题,您只需将 requestedTimeToleranceBefore 和 requestedTimeToleranceAfter 设置为kCMTimeZero,以用于AVAssetImageGenerator.
To resolve this you just need to set requestedTimeToleranceBefore and requestedTimeToleranceAfter to kCMTimeZero for AVAssetImageGenerator.
这篇关于我无法从1.8秒的视频中获得用于生成静止图像的精确CMTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!