从videourl中提取缩略图
我必须从video中提取缩略图(来自url),并使用以下代码:
NSString *stringUrl = video.stringurl; NSURL *url = [NSURL URLWithString:stringUrl]; AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil]; AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset]; [imageGenerator setRequestedTimeToleranceBefore:kCMTimeZero]; [imageGenerator setRequestedTimeToleranceAfter:kCMTimeZero]; CGImageRef imageRef = [imageGenerator copyCGImageAtTime:playerCurrentTime actualTime:&actualtime error:&error]; UIImage *thumbnail = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef);
但有时我有一个copyCGImageAtTime错误,并没有生成缩略图。 错误是: Error save image Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed"(OSStatus error -12792.)", NSLocalizedFailureReason=An unknown error occurred (-12792)}
这里是我已经阅读了解决scheme的链接 ,但如果使用fileURLWithPath:而不是URLWithString:该方法添加到URL的末尾“ – file:// localhost /”,使URL无效。 所以我不知道我能做什么。
如果您使用的是MPMoviePlayerController
则可以使用此代码从videourl生成缩略图。
NSString *stringUrl = video.stringurl; NSURL *url = [NSURL URLWithString:stringUrl]; MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url]; UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
但是,使用此代码,播放器将开始autoplayingaudio。 所以,你必须用这个代码来停止玩家:
//Player autoplays audio on init [player stop];
更新:
错误保存图像错误域= AVFoundationErrorDomain代码= -11800“操作无法完成”(OSStatus错误-12792。)“,NSLocalizedFailureReason =发生未知错误(-12792)}
该错误可能是由于使用URLWithString
。 我想你应该使用-fileURLWithPath
而不是URLWithString
。
示例代码:
NSString *stringUrl = video.stringurl; NSURL *vidURL = [NSURL fileURLWithPath:stringUrl]; AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidURL options:nil]; AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset]; NSError *err = NULL; CMTime time = CMTimeMake(1, 60); CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err]; UIImage *thumbnail = [UIImage imageWithCGImage:imgRef];
我也尝试从HLS可变比特率stream,IE M3U8抓取截图,而且这里提供的方法都不适合我。
我最终成功了。 首先你需要附加一个AVPlayerItemVideoOutput
到你的播放器:
self.playerAV = [AVPlayer playerWithURL:localURL]; NSDictionary* settings = @{ (id)kCVPixelBufferPixelFormatTypeKey : [NSNumber numberWithInt:kCVPixelFormatType_32BGRA] }; AVPlayerItemVideoOutput* output = [[AVPlayerItemVideoOutput alloc] initWithPixelBufferAttributes:settings]; [self.playerAV.currentItem addOutput:output];
现在,当你想抓住截图:
CVPixelBufferRef pixelBuffer = [output copyPixelBufferForItemTime:player.currentTime itemTimeForDisplay:nil]; CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer]; CIContext *temporaryContext = [CIContext contextWithOptions:nil]; CGImageRef videoImage = [temporaryContext createCGImage:ciImage fromRect:CGRectMake(0, 0, CVPixelBufferGetWidth(pixelBuffer), CVPixelBufferGetHeight(pixelBuffer))]; image = [UIImage imageWithCGImage:videoImage]; image = [image cropImageToSize:maxSize withProportionDiffLargerThan:IMAGE_PROPORTION_DIFF]; if ( videoImage ) { CGImageRelease(videoImage); }
在我的应用程序之一,我从这个videourl捕获了一个图像:
MPMoviePlayerController *player = [[[MPMoviePlayerController alloc] initWithContentURL:videoURL]autorelease]; UIImage *thumbnail = [player thumbnailImageAtTime:0.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
只是传递URL对象作为videoURL和使用MPMoviePlayerController我成功地能够有图像的所有时间。 希望你也能用这个简单的代码来做到这一点
如果你使用AVPlayer,你可以得到这样的缩略图:
AVAsset *asset = [AVAsset assetWithURL:sourceURL]; AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset]; CMTime time = CMTimeMake(1, 1); CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL]; UIImage *thumbnail = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); // CGImageRef won't be released by ARC
AVAssetImageGenerator不适用于HSLvideo。
我能够使用下面的示例代码从HSLvideo中获取图像。
示例代码:
CMTime currentTime = _player.currentItem.currentTime; CVPixelBufferRef buffer = [_videoOutput copyPixelBufferForItemTime:currentTime itemTimeForDisplay:nil]; CIImage *ciImage = [CIImage imageWithCVPixelBuffer:buffer]; UIImage *thumbImage = [UIImage imageWithCIImage:ciImage];