I am developing a video based application in Swift3. Where I have one video and a range slider depending on the duration of the video, and the user can select any minimum and maximum value from the slider. If we assume that the user selects a minimum value of 3 seconds and a maximum value of 7 seconds, so for this duration I need to create a thumbnail image of the video. For this, I use AVAssetImageGenerator to generate this, I tried to execute both codes for this:
func createThumbnailOfVideoFromFileURL(_ strVideoURL: URL) -> UIImage?{ let asset = AVAsset(url: strVideoURL) let assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset) assetImgGenerate.appliesPreferredTrackTransform = true let time = CMTimeMake(1, 30) let img = try? assetImgGenerate.copyCGImage(at: time, actualTime: nil) guard let cgImage = img else { return nil } let frameImg = UIImage(cgImage: cgImage) return frameImg }
func generateThumbnailForUrl(vidUrl:URL) -> UIImage { let asset = AVURLAsset(url: vidUrl, options: nil) let imgGenerator = AVAssetImageGenerator(asset: asset) var thmbnlImg = UIImage() do{ let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(0, 1), actualTime: nil) thmbnlImg = UIImage(cgImage: cgImage) thmbnlImg = thmbnlImg.imageRotatedByDegrees(degrees: 90.0, flip: false) } catch{ print(error) }
But the problem is that I get the same thumbnail using both of the above methods, bcos. I do not set the duration here in both methods. How to add a minimum and maximum duration to generate different sketches for each different duration. Please help me solve my problem. Thank!
Edit: I tried to set the duration, for example:
let time: CMTime = CMTimeMakeWithSeconds(rangeSlider!.lowerValue, 1)
Then I get different thumbnails, but for some ranges of sliders I also get a thumbnail image. Can anyone understand how to set preferredTimeScale to CMTimeMakeWithSeconds ?
ios swift video video-thumbnails avasset
Anand Gautam Dec 03 '17 at 9:40 2017-12-03 09:40
source share