dev-labs-bg / swift-video-generator

MIT License
635 stars 121 forks source link

[improvement] thumbnail generation #75

Closed Ludotrico closed 3 years ago

Ludotrico commented 4 years ago

Any chance we can get thumbnail generation from a video?

hamzaozturk commented 3 years ago

You can use this function to get thumbnail from the video.

func thumbnailFromVideoPath(_ path: URL) -> UIImage {
    let asset = AVURLAsset(url: path, options: nil)
    let gen = AVAssetImageGenerator(asset: asset)
    gen.appliesPreferredTrackTransform = true
    let time = CMTimeMakeWithSeconds(0.0, preferredTimescale: 600)
    var actualTime = CMTimeMake(value: 0, timescale: 0)
    let image: CGImage
    do {
        image = try gen.copyCGImage(at: time, actualTime: &actualTime)
        let thumbnail = UIImage(cgImage: image)
        return thumbnail
    } catch { }
    return UIImage()
}