2016-04-28 110 views
1

我有一个需求:在播放视频流之前获取帧图像(m3u8),作为视频预览显示。我使用AVPlayerItemVideoOutpucopyPixelBufferForItemTime:itemTimeForDisplay:只有当播放视频流获取帧图像时,你有其他方法吗?谢谢!!如何在播放m3u8视频流之前获取帧图像? iOS

这是我的代码:

CMTime itemTime = self.playerItem.currentTime; 
CVPixelBufferRef pixelBuffer = [_playerItemVideoOutput copyPixelBufferForItemTime:itemTime 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))]; 

UIImage *uiImage = [UIImage imageWithCGImage:videoImage]; 
CGImageRelease(videoImage); 
NSLog(@"uiImage:%@", uiImage); 

回答

2

正如你在你的问题说,AVPlayerItemVideoOutput是要走的路,但是,视频不需要被打,只是需要“准备就绪”。

我创建了一个简单易用的实用程序,遵循此原则从远程流中提取缩略图。 (https://github.com/acotilla91/ACThumbnailGenerator

如何使用:

double bitRate = 1000000; // force video bit rate (can be use to cap video quality and improve performance). Pass 0 to use default bit rate. 
self.thumbnailGenerator = [[ACThumbnailGenerator alloc] initWithPreferredBitRate:bitRate]; 

NSURL *videoURL = [NSURL URLWithString:@"http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8"]; 
int position = 10; // video position (in seconds) from where thumbnail should be extracted. Always pass 0 for live streams. 

[self.thumbnailGenerator loadImageFrom:videoURL position:position withCompletionBlock:^(UIImage *image) { 

    // use `image`    

}]; 

希望它能帮助。