2

I need to show Video Preview Thumbanils while seeking in AVPlayer. Just like how youtube shows them. (iOS) (Also for live streaming) (Solution preferred more for Wowza/Amazon S3)

  • Does this answer your question? [iOS - How to get thumbnail from video without play?](https://stackoverflow.com/questions/32691304/ios-how-to-get-thumbnail-from-video-without-play) – haider_kazal Feb 14 '20 at 09:07

2 Answers2

1

You can try this:

extension AVAsset {

    func getPreviewImage(for timeInSeconds: Int = 0) -> UIImage? {
        let imageGenerator = AVAssetImageGenerator(asset: self)
        imageGenerator.requestedTimeToleranceBefore = .zero
        imageGenerator.requestedTimeToleranceAfter = .zero
        imageGenerator.appliesPreferredTrackTransform = true
        guard let cgImage = try? imageGenerator.copyCGImage(at: CMTime(value: CMTimeValue(timeInSeconds), timescale: 1), actualTime: nil) else { return nil }
        return UIImage(cgImage: cgImage)
    }

}

You shouldn't get the preview in the main thread.

If you require a series of preview images, you can try this:

If you require a series of images, you can achieve far greater efficiency using the asynchronous method, generateCGImagesAsynchronously(forTimes:completionHandler:), which employs decoding efficiencies similar to those used during playback.

Vladislav Markov
  • 587
  • 1
  • 5
  • 19
0

You can try this Youtube helper instead - https://github.com/youtube/youtube-ios-player-helper

Ami Patel
  • 71
  • 6