如何在播放m3u8视频流之前获取帧图像? iOS版

我有一个要求:在播放视频流(m3u8)之前获取帧图像,作为视频预览显示.我使用AVPlayerItemVideoOutpu copyPixelBufferForItemTime:itemTimeForDisplay:只有在播放视频流才能获得帧图像时,您还有其他方法吗?谢谢!!

这是我的代码:

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);

最佳答案 编辑:

我已将此项目转换为Swift.这是新的回购
ACThumbnailGenerator-Swift.

用法:

var generator: ACThumbnailGenerator!

func captureSomeImage() {
    let streamUrl = URL(string: "https://p-events-delivery.akamaized.net/18oijbasfvuhbfsdvoijhbsdfvljkb6/m3u8/hls_vod_mvp.m3u8")!
    generator = ACThumbnailGenerator(streamUrl: streamUrl)
    generator.delegate = self
    generator.captureImage(at: 300)
}

func generator(_ generator: ACThumbnailGenerator, didCapture image: UIImage, at position: Double) {
    // Use `image`
}

原始答案:

正如你在问题中所述,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`            

}];

希望能帮助到你.

点赞