无法使用Fairplay HLS从AVPlayer获取视频缩略图 [英] Failed to get video thumbnail from AVPlayer using Fairplay HLS
问题描述
我正在尝试为tvOS中的视频播放器应用构建自定义进度栏,并希望在用户扫描视频时显示视频的缩略图.
I'm trying to build a custom progress bar for a video player app in tvOS, and would like to show thumbnails of the video while the user scans the video.
我正在使用AVPlayer
和Fairplay HLS播放远程视频文件.我尝试使用2种方法来执行此操作.一个使用AVAssetImageGenerator
的copyCGImage
,另一个使用AVPlayerItemVideoOutput
的copyPixelBuffer
方法.两者都返回nil.
I'm using AVPlayer
and Fairplay HLS to play remote video files. I've tried to do this using 2 methods. One with AVAssetImageGenerator
's copyCGImage
, and the other with AVPlayerItemVideoOutput
's copyPixelBuffer
method. Both return nil.
当我尝试本地视频文件时,第一种方法有效.
When I tried with a local video file, the first method worked.
方法1:
let imageGenerator = AVAssetImageGenerator(asset: playerItem.asset)
let progressSeconds = playerItem.duration.seconds * Double(progress)
let time = CMTime(seconds: progressSeconds, preferredTimescale: 5)
if let imageRef = try? imageGenerator.copyCGImage(at: time, actualTime: nil) {
image = UIImage(cgImage:imageRef)
}
方法2:
let videoThumbnailsOutput = AVPlayerItemVideoOutput(pixelBufferAttributes: [String(kCVPixelBufferPixelFormatTypeKey): NSNumber(value: kCVPixelFormatType_32BGRA)])
player?.currentItem?.add(videoThumbnailsOutput)
if let pixelBuffer = videoThumbnailsOutput.copyPixelBuffer(forItemTime: time, itemTimeForDisplay: nil) {
let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
有什么想法我做错了吗,或者还有其他方法吗?
Any ideas what I'm doing wrong or is there any other way?
谢谢!
推荐答案
这通常是通过利用与您的实际流关联的特技播放流来完成的.
This is usually done by making use of the trick play stream associated to your actual stream.
https://en.wikipedia.org/wiki/Trick_mode
您可以在HLS流的清单中找到用键EXT-X-I-FRAME-STREAM-INF声明的声明.可能需要使用正则表达式才能解析其值.
You can find it declared with the key EXT-X-I-FRAME-STREAM-INF in the manifest of your HLS stream. A regex might be needed in order to parse its value.
"#EXT-X-I-FRAME-STREAM-INF[^#]*URI=[^#]*"
一旦有了特技播放流的URL,就可以将AVPlayer的暂停实例用作缩略图.而且,当用户左右滑动时,您应该在缩略图中寻找播放器以显示右图框.
Once you have the URL of the trick play stream, you can use a paused instance of AVPlayer as a thumbnail. And when the user swipe left and right, you should seek the player in the thumbnail to show the right frame.
这篇关于无法使用Fairplay HLS从AVPlayer获取视频缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!