I have a FairPlay-encrypted HLS stream and played the video in an AVPlayer.And I want to generate scrubbing thumbnails using the AVAssetImageGenerator. Also, I am able to generate thumbnails for clear streams but get errors for protected content.
*How to generate thumbnails for protected content.
func getImageThumbnail(forTime: CMTime) {
let generator = AVAssetImageGenerator(asset: asset)
generator.appliesPreferredTrackTransform = true
generator.cancelAllCGImageGeneration()
generator.generateCGImagesAsynchronously(forTimes: [NSValue(time: forTime)]) { [weak self] requestedTime, image, actualTime, result, error in
if let error = error {
print("Error generate: \(error.localizedDescription)")
return
}
if let image = image {
DispatchQueue.main.async {
let image = UIImage(cgImage: image).jpegData(compressionQuality: 1.0)
self?.playerImg.image = UIImage(data: image!)
}
}
}
}
Hello @narendra1098, this is not possible. When an HLS stream is encrypted, AVAssetImageGenerator
will not have access to decoded frames. The recommended way to build a thumbnail scrubber for encrypted content is to instantiate a second AVPlayer
with an AVPlayerLayer
sized to the thumbnail. In order to get it to display thumbnails, keep the player rate at zero and use seek(to:toleranceBefore:toleranceAfter:completionHandler:) to seek to the thumbnail position. Pass a finite, non-zero value for toleranceBefore
and toleranceAfter
. For best performance, while the user is moving the scrubber, wait for each seek to complete before issuing the next seek, to avoid overloading the network.