5

每次我尝试从我的视频资产生成静止帧时,它都会在 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

…………………………………………………………………………………………

非常感谢你们提前.. :)

4

2 回答 2

23

要解决此问题,您只需将requestedTimeToleranceBeforerequestedTimeToleranceAfter设置为 AVAssetImageGenerator 的 kCMTimeZero。

AVAssetImageGenerator 类参考

于 2011-11-07T15:19:23.577 回答
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 帧!!

于 2011-04-29T14:50:52.660 回答