获取远程视频的预览图

Get preview image for remote video

我有一个 link 的视频(例如:http://example.com/video.mp4)。我需要获取视频的第一帧。 我使用:

func getPreviewImage(_ url: URL) -> UIImage? {
    let asset = AVURLAsset(url: url)
    let generator = AVAssetImageGenerator(asset: asset)
    generator.appliesPreferredTrackTransform = true
    do {
        let cgImage = try generator.copyCGImage(at: CMTimeMake(0, 1), actualTime: nil)
        return UIImage(cgImage: cgImage)
    } catch {
        print(error)
    }
    return nil
}

但是,这不起作用。 我在日志中收到以下错误:

Code=-1102 "You do not have permission to access the requested resource.

告诉我如何获取第一帧?

试试这个。

func getThumbnailImageFromVideoURL(fromUrl url: URL) -> UIImage? {
    let asset: AVAsset = AVAsset(url: url)
    let imageGenerator = AVAssetImageGenerator(asset: asset)

    do {
        let thumbnailImage = try imageGenerator.copyCGImage(at: CMTimeMake(value: 1, timescale: 60) , actualTime: nil)
        return UIImage(cgImage: thumbnailImage)
    } catch let error {
        print(error)
    }

    return nil
}