Getting the same thumbnail image of videos each time from Url in Swift iOS

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) } // !! check the error before proceeding return thmbnlImg } 

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 ?

0
ios swift video video-thumbnails avasset
Dec 03 '17 at 9:40
source share
1 answer

Suppose your cropped videoURL video videoURL . After successfully trimming the video, add this piece of code. In fact, this piece of code will help you retrieve images from the cropped video every second (this means that if the duration of your cropped video is 10 seconds, this code will extract 10 images every second, and all these images are saved in an array called videoFrames ) . Finally, you can do whatever you want with these images. You can also add an activity indicator during this process. Hope this helps.

 var videoFrames = [UIImage]() let asset : AVAsset = AVAsset(url: videoURL as URL) let videoDuration = CMTimeGetSeconds(asset.duration) let integerValueOFVideoDuration = Int(videoDuration) //start activity indicator here for index in 0..<integerValueOFVideoDuration + 1 { self.generateFrames(url: videoURL, fromTime: Float64(index)) } func generateFrames(url: NSURL, fromTime: Float64) { if videoFrames.count == integerValueOFVideoDuration { //end activity indicator here return } let asset: AVAsset = AVAsset(url: url as URL) let assetImgGenerate: AVAssetImageGenerator = AVAssetImageGenerator(asset: asset) assetImgGenerate.maximumSize = CGSize(width: 300, height: 300) assetImgGenerate.appliesPreferredTrackTransform = true let time: CMTime = CMTimeMakeWithSeconds(fromTime, 600) var img: CGImage? do { img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil) } catch { } if img != nil { let frameImg: UIImage = UIImage(cgImage: img!) videoFrames.append(frameImg) } else { //return nil } } 
-one
Dec 03 '17 at 11:16
source share



All Articles