I am trying to acquire a thumbnail from a video url. The video is a stream (HLS) with the m3u8 format. I've already tried requestThumbnailImagesAtTimes from the MPMoviePlayerController, but that didn't work. Does anyone have a solution for that problem? If so how'd you do it?
9 Answers
If you don't want to use MPMoviePlayerController
, you can do this:
AVAsset *asset = [AVAsset assetWithURL:sourceURL];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
CMTime time = CMTimeMake(1, 1);
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef); // CGImageRef won't be released by ARC
Here's an example in Swift:
func thumbnail(sourceURL sourceURL:NSURL) -> UIImage {
let asset = AVAsset(URL: sourceURL)
let imageGenerator = AVAssetImageGenerator(asset: asset)
let time = CMTime(seconds: 1, preferredTimescale: 1)
do {
let imageRef = try imageGenerator.copyCGImageAtTime(time, actualTime: nil)
return UIImage(CGImage: imageRef)
} catch {
print(error)
return UIImage(named: "some generic thumbnail")!
}
}
I prefer using AVAssetImageGenerator
over MPMoviePlayerController
because it is thread-safe, and you can have more than one instantiated at a time.
-
3The CGImageRef you get from -copyCGImageAtTime:actualTime:error: needs to be released with CGImageRelease();– rbrownDec 7, 2012 at 18:29
-
1Gives me the error AVErrorOperationNotSupportedForAsset . plz help Aug 1, 2013 at 12:07
-
4just as a side note... AV needs the AVFoundationFramework and CMTime needs CoreMediaFramework (had a little headache with this ones) cheers! Aug 24, 2013 at 3:49
-
-
1perfect...this is a good alternative for iOS 7 as thumbnailImageAtTime is now deprecated. Nov 2, 2013 at 20:45
Acquire a thumbnail from a video url.
NSString *strVideoURL = @"http://www.xyzvideourl.com/samplevideourl";
NSURL *videoURL = [NSURL URLWithString:strVideoURL] ;
MPMoviePlayerController *player = [[[MPMoviePlayerController alloc] initWithContentURL:videoURL]autorelease];
UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
player = nil;
Replace your video URL string with strVideoURL
.
You will get thumbnail as a output from video
.
And thumbnail is UIImage
type data !
-
2i am using this code for the thumbnail of video from the url of youtube but it is not giving me the image.i am using to put the thumbnail in the table view so can you help me Oct 4, 2012 at 8:26
-
I think you can't get the video from Youtube URL like this www.youtube.com/watch?v=xxxxxxx Oct 29, 2012 at 4:26
-
I'm using this url sciencentral.com/news/image_db/2024515/… I get nil thumbnail image. please help Aug 7, 2013 at 12:23
-(UIImage *)loadThumbNail:(NSURL *)urlVideo
{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:urlVideo options:nil];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generate.appliesPreferredTrackTransform=TRUE;
NSError *err = NULL;
CMTime time = CMTimeMake(1, 60);
CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
NSLog(@"err==%@, imageRef==%@", err, imgRef);
return [[UIImage alloc] initWithCGImage:imgRef];
}
Add AVFoundation
framework in your project and don't forget to import <AVFoundation/AVFoundation.h>
and you have to pass the path of your video saved in document directory as a parameter and receive the image as UIImage
.
-
I have not saved the video in Documents Directory, and I do not want to save. I want to generate image from URL. Is it possible ? Mar 17, 2017 at 11:48
-
Hello @ArpitBParekh, It is not possible to generate thumbnail online i.e. without saving in document directory. Please accept my answer if it works for you :) Mar 20, 2017 at 4:38
-
thanks for reply. But after searching I did it. Let me post the link. Mar 20, 2017 at 6:24
-
stackoverflow.com/questions/37343358/… I do not know, but it worked. Mar 20, 2017 at 6:26
-
For few seconds UI stucks. But If Ic all the below code on NSTimer (Another thread) , ?Ui des not stuck. Mar 20, 2017 at 6:26
you can get the thumbnail from your video url with the use of below code
MPMoviePlayerController *player = [[[MPMoviePlayerController alloc] initWithContentURL:videoURL]autorelease];
UIImage *thumbnail = [player thumbnailImageAtTime:0.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
-
1Technically deprecated in iOS 7, but it still works, and I could never figure out how to use – requestThumbnailImagesAtTimes:timeOption: properly– Rob R.Oct 2, 2013 at 16:28
-
4
Maybe this is useful for someone else who faces the same problem. I needed an easy solution for creating a thumbnail for Images, PDFs and Videos. To solve that problem I've created the following Library (in Swift).
https://github.com/prine/ROThumbnailGenerator
The usage is very straightforward: var thumbnailImage = ROThumbnail.getThumbnail(url)
It has internally three different implementations and depending on the file extension it does create the thumbnail. You can easily add your own implementation if you need a thumbnail creator for another file extension.
Swift 3 Version :
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 {
// Set a default image if Image is not acquired
return UIImage(named: "ico_placeholder")
}
}
For Swift 3.0:
func generateThumbnailForVideoAtURL(filePathLocal: NSString) -> UIImage? {
let vidURL = NSURL(fileURLWithPath:filePathLocal as String)
let asset = AVURLAsset(url: vidURL as URL)
let generator = AVAssetImageGenerator(asset: asset)
generator.appliesPreferredTrackTransform = true
let timestamp = CMTime(seconds: 1, preferredTimescale: 60)
do {
let imageRef = try generator.copyCGImage(at: timestamp, actualTime: nil)
let frameImg : UIImage = UIImage(cgImage: imageRef)
return frameImg
} catch let error as NSError {
print("Image generation failed with error ::\(error)")
return nil
}
}
For Swift 5 you can get it this way:
First import AVKit or AVFoundation
to ViewController
import AVKit
Code:
// Get Thumbnail Image from URL
private func getThumbnailFromUrl(_ url: String?, _ completion: @escaping ((_ image: UIImage?)->Void)) {
guard let url = URL(string: url ?? "") else { return }
DispatchQueue.main.async {
let asset = AVAsset(url: url)
let assetImgGenerate = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMake(value: 2, timescale: 1)
do {
let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
let thumbnail = UIImage(cgImage: img)
completion(thumbnail)
} catch {
print("Error :: ", error.localizedDescription)
completion(nil)
}
}
}
Usage
@IBOutlet weak imgThumbnail: ImageView!
and then call getThumbnailFromUrl
method and pass URL
as a Sting
self.getThumbnailFromUrl(videoURL) { [weak self] (img) in
guard let `self` = self else { return }
if let img = img {
self.imgThumbnail.image = img
}
}
Thank you
Swift 2 code with AVAssetImageGenerator:
func thumbnailImageForVideo(url:NSURL) -> UIImage?
{
let asset = AVAsset(URL: url)
let imageGenerator = AVAssetImageGenerator(asset: asset)
imageGenerator.appliesPreferredTrackTransform = true
var time = asset.duration
//If possible - take not the first frame (it could be completely black or white on camara's videos)
time.value = min(time.value, 2)
do {
let imageRef = try imageGenerator.copyCGImageAtTime(time, actualTime: nil)
return UIImage(CGImage: imageRef)
}
catch let error as NSError
{
print("Image generation failed with error \(error)")
return nil
}
}