IOS - How to get a thumbnail from a video without a game?

I am trying to get a sketch from a video and show it in my desk. Here is my code:

- (UIImage *)imageFromVideoURL:(NSURL *)contentURL { AVAsset *asset = [AVAsset assetWithURL:contentURL]; // Get thumbnail at the very start of the video CMTime thumbnailTime = [asset duration]; thumbnailTime.value = 25; // Get image from the video at the given time AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; CGImageRef imageRef = [imageGenerator copyCGImageAtTime:thumbnailTime actualTime:NULL error:NULL]; UIImage *thumbnail = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); return thumbnail; } 

But the image always returns in black. What's wrong?

+9
ios image video-thumbnails
Sep 21 '15 at 9:08
source share
7 answers

Use this: Swift 3 -

 func createThumbnailOfVideoFromFileURL(videoURL: String) -> UIImage? { let asset = AVAsset(url: URL(string: videoURL)!) let assetImgGenerate = AVAssetImageGenerator(asset: asset) assetImgGenerate.appliesPreferredTrackTransform = true let time = CMTimeMakeWithSeconds(Float64(1), 100) do { let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil) let thumbnail = UIImage(cgImage: img) return thumbnail } catch { return UIImage(named: "ico_placeholder") } } 

Important Note:

You will need to use this in if else, as it is resource-rich. You will need to save the image in an array or model and verify that if the sketch you created once refers to the cache array, so that cellForRowAtIndexPath does not cause a lag in the scroll of your UITableView

+7
Mar 24 '17 at 10:01
source share
 //(Local URL) NSURL *videoURL = [NSURL fileURLWithPath:filepath];// filepath is your video file path AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil]; AVAssetImageGenerator *generateImg = [[AVAssetImageGenerator alloc] initWithAsset:asset]; NSError *error = NULL; CMTime time = CMTimeMake(1, 1); CGImageRef refImg = [generateImg copyCGImageAtTime:time actualTime:NULL error:&error]; NSLog(@"error==%@, Refimage==%@", error, refImg); UIImage *frameImage= [[UIImage alloc] initWithCGImage:refImg]; return frameImage; 
+3
Dec 30 '15 at 5:00
source share

Just use this code. Submit your video URL and get an image.

 +(UIImage *)getPlaceholderImageFromVideo:(NSString *)videoURL { NSURL *url = [NSURL URLWithString:videoURL]; AVAsset *asset = [AVAsset assetWithURL:url]; AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; CMTime time = [asset duration]; time.value = 0; CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL]; UIImage *thumbnail = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); return thumbnail; } 

Hope this is what you are looking for. I care about any concern. :)

+3
Jul 18 '17 at 10:47
source share

Please check the link "Create thumbnails from the video"

https://littlebitesofcocoa.com/115-generating-thumbnails-from-videos

For an application, it is enough to distribute one or several thumbnails (small previews of still images) of what is in the video. However, depending on where the video comes from, we may not have easy access to pre-made thumbnails for it. Let's see how we can use AVAssetImageGenerator to capture our own. We start with a simple NSURL for video, it can be local or remote. We will create an AVAsset with it and with a new AVAssetImageGenerator object. We will configure the generator to apply the preferred transforms so that our sketches are in the correct orientation.

 import AVFoundation if let asset = AVAsset(URL: videoURL) { let durationSeconds = CMTimeGetSeconds(asset.duration) let generator = AVAssetImageGenerator(asset: asset) generator.appliesPreferredTrackTransform = true let time = CMTimeMakeWithSeconds(durationSeconds/3.0, 600) var thumbnailImage: CGImageRef generator.generateCGImagesAsynchronouslyForTimes([NSValue(CMTime: time)]) { (requestedTime: CMTime, thumbnail: CGImage?, actualTime: CMTime, result: AVAssetImageGeneratorResult, error: NSError?) in self.videoThumbnailImageView.image = UIImage(CGImage: thumbnail) } } 
+1
Apr 13 '17 at 9:48 on
source share

Swift 4 code for @Disha's answer:

 let imageGenerator = AVAssetImageGenerator(asset: avAsset) let time = CMTime(seconds: seconds, preferredTimescale: 600) let times = [NSValue(time: time)] imageGenerator.generateCGImagesAsynchronously(forTimes: times, completionHandler: { requestedTime, image, actualTime, result, error in guard let cgImage = image else { print("No image!") return } let uiImage = UIImage(cgImage: cgImage) UIImageWriteToSavedPhotosAlbum(uiImage, nil, nil, nil); }) 
+1
Jan 22 '19 at 20:07
source share

Using Swift 5 as an extension function on AVAsset:

 import AVKit extension AVAsset { func generateThumbnail(completion: @escaping (UIImage?) -> Void) { DispatchQueue.global().async { let imageGenerator = AVAssetImageGenerator(asset: self) let time = CMTime(seconds: 0.0, preferredTimescale: 600) let times = [NSValue(time: time)] imageGenerator.generateCGImagesAsynchronously(forTimes: times, completionHandler: { _, image, _, _, _ in if let image = image { completion(UIImage(cgImage: image)) } else { completion(nil) } }) } } } 

Using:

  AVAsset(url: url).generateThumbnail { [weak self] (image) in DispatchQueue.main.async { guard let image = image else { return } self?.imageView.image = image } } 
+1
Apr 05 '19 at 8:16
source share
 func getThumbnailFrom(path: URL) -> UIImage? { do { let asset = AVURLAsset(url: path , options: nil) let imgGenerator = AVAssetImageGenerator(asset: asset) imgGenerator.appliesPreferredTrackTransform = true let timestamp = asset.duration print("Timestemp: \(timestamp)") let cgImage = try imgGenerator.copyCGImage(at: timestamp, actualTime: nil) let thumbnail = UIImage(cgImage: cgImage) return thumbnail } catch let error { print("*** Error generating thumbnail: \(error.localizedDescription)") return nil } } 

This code works.

0
Jun 17 '19 at 6:57
source share



All Articles