ios - 如何从 Swift 中的 .m3u8 格式视频 url 获取缩略图?

标签 ios swift tvos avplayeritem

func getThumbnailFrom(path: URL?, withCompletionHandler completion:@escaping ((UIImage)-> Void)){
        var thumbnail = UIImage(named: "name of Dummy Image")

        if let url = path{
            DispatchQueue.global(qos: .background).async {
                do{
                    let asset = AVURLAsset(url: url , options: nil)
                    let imgGenerator = AVAssetImageGenerator(asset: asset)
                    imgGenerator.appliesPreferredTrackTransform = true
                    let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(value: 500,timescale: 30), actualTime: nil)
                    thumbnail = UIImage(cgImage: cgImage)
                    completion(thumbnail!)
                }catch let error{
                      print(error.localizedDescription)
                }
            }
        }
    completion(thumbnail!)
    }

Used it as 

    getThumbnailFrom(path: URL(string: "https://p-events-delivery.akamaized.net/18oijbasfvuhbfsdvoijhbsdfvljkb6/m3u8/hls_vod_mvp.m3u8")!, withCompletionHandler: { (image) in DispatchQueue.main.async(execute: { self.imagePreview.image = image }) })

也试试 https://github.com/acotilla91/ACThumbnailGenerator-Swift 但在这个唯一的演示链接中是有效的

有什么解决办法吗?

提前致谢。

最佳答案

如果你打算使用 ACThumbnailGenerator-Swift,那么在 Info.plist 中添加 NSAppTransportSecurity

关于ios - 如何从 Swift 中的 .m3u8 格式视频 url 获取缩略图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56661015/

相关文章:

ios - 大型 NSDictionary 的 objectForKey 是否慢?

ios - Xcode 6 无法子类化 UIStoryboardSegue

ios - 代码 -6003 是否有类似 NSURLError 枚举的常量?

ios - 重新加载 UIPickerView 崩溃

swift - 适用于 tvOS 的首选焦点 View

ios - 在 Swift 中将 Int 转换为 Float 会丢失大数字的精度

ios - 如何检测 iOS 9 中弹出窗口何时消失

ios - CoreBluetooth 框架是否与 BLE 5.0 设备(外围设备)兼容?

swift - UICollectionView 单元格间距相等

tableview - TVos 中 tableview 单元格之间的空间