I can't seem to reliably fetch a thumbnail image from a video URL in Swift 5. Sometimes it works, but fails more often than not. If you can help, please do. Thanks in advance. (I've tried this both on simulator & a real device)
Edit: I checked the whole error, as opposed to merely the localizedDescription, and I got:
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-12792), NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x281998a80 {Error Domain=NSOSStatusErrorDomain Code=-12792 "(null)"}}
Note- my app is allowing arbitrary loads in info.plist
static func getThumbnailImageFromVideoUrl(url: URL, completion: @escaping ((_ image: UIImage?)->Void)) {
DispatchQueue.global().async {
let asset = AVAsset(url: url)
let avAssetImageGenerator = AVAssetImageGenerator(asset: asset)
avAssetImageGenerator.appliesPreferredTrackTransform = true
let thumbnailTime = CMTimeMake(value: 2, timescale: 1)
do {
let cgThumbImage = try avAssetImageGenerator.copyCGImage(at: thumbnailTime, actualTime: nil)
let thumbImage = UIImage(cgImage: cgThumbImage)
DispatchQueue.main.async {
completion(thumbImage)
}
} catch {
print("Thumbnail not fetched, with error: \"\(error.localizedDescription)\"")
DispatchQueue.main.async {
completion(nil)
}
}
}
}