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)
Asked
Active
Viewed 1,257 times
2
-
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 Answers
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
-
The thing is this approach doesn't provide me smooth scrolling or on the go thumbnail display. (It takes a bit to get the particular thumbnail at a particular time) – Raj Pahelajani Feb 14 '20 at 09:06
-
@RajPahelajani Do you get the preview in a background thread? – Vladislav Markov Feb 14 '20 at 09:20
-
I tried that but it's not what I expect, Also for the videos with extension 'm3u8' this approach doesn't provide me the thumbnails. – Raj Pahelajani Feb 14 '20 at 09:25
-
@RajPahelajani I see an edited post. Maybe it's because of the live streaming, I don’t know. I use this for regular videos. – Vladislav Markov Feb 14 '20 at 09:30
-
1@RajPahelajani I updated my answer. For greater efficiency, you can try `generateCGImagesAsynchronously`. I have not found out yet why this does not work with the extension "m3u8". – Vladislav Markov Feb 14 '20 at 09:52
-
You should try getting thumbnai from thread other than main thread. – Abhiraj Kumar Feb 14 '20 at 13:10
0
You can try this Youtube helper instead - https://github.com/youtube/youtube-ios-player-helper

Ami Patel
- 71
- 6
-
This is only for playing youtube videos and not other videos and live streams. – Raj Pahelajani Feb 14 '20 at 10:49