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

2

5

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.. :)

Campestral answered 28/4, 2011 at 22:58 Comment(1)
I just tested a 6 second clip.. When I request image at 2 seconds, I still get my image at 0.00.. when I request image at 4 seconds, I finally get an image at 5.003 seconds! So my code is working, I just need much more precision!!Campestral
N
24

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

AVAssetImageGenerator Class Reference

Niela answered 7/11, 2011 at 15:19 Comment(1)
This is the best suggestion, at least in my opinion!Hannus
C
5

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!!

Campestral answered 29/4, 2011 at 14:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.