pgeof
pgeof

Reputation: 41

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.

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.

This work really fine on iOS simulator, the rendered image is perfect. 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 ?

Thank for any help.

Upvotes: 1

Views: 2572

Answers (2)

Mensly
Mensly

Reputation: 101

I was able to find a way to save a screenshot of an HLS live stream, by adding an AVPlayerItemVideoOutput object to the AVPlayerItem.

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.

Upvotes: 2

onekiloparsec
onekiloparsec

Reputation: 2063

People have tried, and failed (like me), apparently because of the nature of HLS. See: http://blog.denivip.ru/index.php/2012/12/screen-capture-in-ios/?lang=en

Upvotes: 0

Related Questions