简体   繁体   English

使用iOS设备从HLS视频流获取屏幕截图

[英]Take screenshot from HLS video stream with iOS device

I'm developing an application which plays an HLS video and rendering it in an UIView. 我正在开发一个播放HLS视频并将其呈现在UIView中的应用程序。

At a determinate time I want to save a picture of the currently displayed video image. 我想在确定的时间内保存当前显示的视频图像的图片。 For this I begin an image context graphic, draw the UIView hierarchy in the context and save it in an UIImage with UIGraphicsGetImageFromCurrentImageContext method. 为此,我开始一个图像上下文图形,在上下文中绘制UIView层次结构,然后使用UIGraphicsGetImageFromCurrentImageContext方法将其保存在UIImage中。

This work really fine on iOS simulator, the rendered image is perfect. 在iOS模拟器上,这项工作真的很好,渲染的图像非常完美。 But on a device the rendered image is totally white. 但是在设备上,渲染的图像完全是白色的。

Anyone knows why it doesn't work on device ? 有人知道为什么它在设备上不起作用吗? Or, is there a working way to take a screenshot of an HLS video on device ? 或者,是否有一种可行的方法来在设备上拍摄HLS视频的屏幕截图?

Thank for any help. 感谢您的帮助。

I was able to find a way to save a screenshot of an HLS live stream, by adding an AVPlayerItemVideoOutput object to the AVPlayerItem. 通过向AVPlayerItem添加AVPlayerItemVideoOutput对象,我能够找到一种方法来保存HLS实时流的屏幕截图。

In initialisation: 在初始化中:

self.output = AVPlayerItemVideoOutput(pixelBufferAttributes: 
                                      Dictionary<String, AnyObject>())
playerItem.addOutput(output!)

To save screenshot: 要保存屏幕截图:

guard let time = self.player?.currentTime() else { return }
guard let pixelBuffer = self.output?.copyPixelBufferForItemTime(time,
                        itemTimeForDisplay: nil) else { return }
let ciImage = CIImage(CVPixelBuffer: pixelBuffer)
let temporaryContext = CIContext(options: nil)
let rect = CGRectMake(0, 0, 
                      CGFloat(CVPixelBufferGetWidth(pixelBuffer)), 
                      CGFloat(CVPixelBufferGetHeight(pixelBuffer)))
let videoImage = temporaryContext.createCGImage(ciImage, fromRect: rect)
let image = UIImage(CGImage: videoImage)
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)

This seems not to work in the simulator, but works fine on a device. 这似乎在模拟器中不起作用,但在设备上可以正常工作。 Code is in Swift 2 but should be straightforward to convert to obj-c or Swift 1.x. 代码在Swift 2中,但是应该很容易转换为obj-c或Swift1.x。

People have tried, and failed (like me), apparently because of the nature of HLS. 人们尝试过并且失败了(就像我一样),显然是因为HLS的本质。 See: http://blog.denivip.ru/index.php/2012/12/screen-capture-in-ios/?lang=en 请参阅: http//blog.denivip.ru/index.php/2012/12/screen-capture-in-ios/?lang = zh-CN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM