Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I cannot get a precise CMTime for Generating Still Image from 1.8 second Video

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));

And my Console reads..

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] The time I want my image is 1.000000

2011-04-28 18:49:59.064 videoTest[26553:207] The actual time my image was take was 0.000000

..........................

Thank you guys so much in advance.. :)

like image 480
johntraver Avatar asked Apr 28 '11 22:04

johntraver


1 Answers

To resolve this you just need to set requestedTimeToleranceBefore and requestedTimeToleranceAfter to kCMTimeZero for AVAssetImageGenerator.

AVAssetImageGenerator Class Reference

like image 164
kibermaks Avatar answered Jan 03 '23 16:01

kibermaks