如何在Swift中从.m3u8格式的视频URL获取缩略图图像?

8
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个回答

3
如果你要使用ACThumbnailGenerator-Swift,则需要在Info.plist中添加NSAppTransportSecurity

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接