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开发者_Go百科 %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.. :)
To resolve this you just need to set requestedTimeToleranceBefore and requestedTimeToleranceAfter to kCMTimeZero for AVAssetImageGenerator.
AVAssetImageGenerator Class Reference
Late last night I had an Idea and sure enough it worked this morning. Essentially I just create a new Composition Asset and then create a time range that represents one frame for Video at 24 frames per second. Once I have these compositions created, I just grab the first frame of each comp. I do this for each frame and create an array holding all my frames. Here is what I did..
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]);
Then The Console Reads..
2011-04-29 10:42:24.292 videoTest[29019:207] This video is calculated at 45.120000 Frames..
2011-04-29 10:42:24.293 videoTest[29019:207] You made a total of 45 Frames!!
精彩评论