如何在播放 m3u8 视频流之前获取帧图像? iOS
Posted
技术标签:
【中文标题】如何在播放 m3u8 视频流之前获取帧图像? iOS【英文标题】:How to get the frame image before play m3u8 video streaming? iOS 【发布时间】:2016-08-22 14:13:56 【问题描述】:我有一个需求:在播放视频流(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);
【问题讨论】:
【参考方案1】:编辑: 我已将此项目转换为 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`
];
希望对你有帮助。
【讨论】:
安卓兄弟呢?以上是关于如何在播放 m3u8 视频流之前获取帧图像? iOS的主要内容,如果未能解决你的问题,请参考以下文章