0

嗨,我想从我的视频中创建一个缩略图,长度为 2.46 分钟,大小为 68 Mb。我使用此代码创建缩略图:

AVAsset *asset = [AVAsset assetWithURL:videoURL];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
CMTime time = [asset duration];
time.value = 0;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef); 

return thumbnail;

问题是缩略图始终为零,但如果我将视频长度减少到 15 秒,它将创建缩略图。我可以为超过 15 秒的电影创建缩略图吗?

谢谢

4

1 回答 1

0

嗯,这种使用 AVAssetImageGenerator 的方式有效:

AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:videoURL options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;

CGFloat videoLenght=((float)asset.duration.value)/((float)asset.duration.timescale);
CMTime thumbTime = CMTimeMakeWithSeconds(videoLenght,2.0);

AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
    if (result != AVAssetImageGeneratorSucceeded) {
        NSLog(@"couldn't generate thumbnail, error:%@", error);
    }else{
        UIImage *image=[self.class scaledImage:[UIImage imageWithCGImage:im]];
        [self performSelectorOnMainThread:@selector(setVideoImage:) withObject:image  waitUntilDone:NO];
    }
};

[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];  

(videoLenght,2.0) 是快照发生的时间,在这种情况下是视频的一半,我不建议您使用固定的截图时间,因为如果时间超出范围,图像将为零。

于 2015-10-26T02:09:08.007 回答