使用此方法:
class func getVideoInfo(from url: URL,
completion: @escaping ((_ image: UIImage?, _ duration: CGFloat?) -> Void)) {
DispatchQueue.global().async {
let asset = AVAsset(url: url)
let duration = CMTimeGetSeconds(asset.duration)
let avAssetImageGenerator = AVAssetImageGenerator(asset: asset)
avAssetImageGenerator.appliesPreferredTrackTransform = true
let thumnailTime = CMTimeMake(value: 2, timescale: 1)
do {
let cgThumbImage = try avAssetImageGenerator.copyCGImage(at: thumnailTime, actualTime: nil)
let thumbNailImage = UIImage(cgImage: cgThumbImage)
DispatchQueue.main.async {
completion(thumbNailImage, duration)
}
} catch {
DispatchQueue.main.async {
completion(nil, nil)
}
}
}
}