如何从Swift中的.m3u8格式视频网址获取缩略图?

时间:2019-06-19 06:07:27

标签: 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 但是只有这个演示链接有效

有什么解决办法吗?

谢谢。

1 个答案:

答案 0 :(得分:3)

如果要使用Debug而不是在Info.plist中添加ACThumbnailGenerator-Swift