I want to get thumbnail images of every frame from video and then save this images in Mutable Array of images.
I want to use this images to play as a animation.
NSURL* assetURL = [self.asset valueForProperty:ALAssetPropertyAssetURL];
NSDictionary* assetOptions = nil;
AVAsset* myAsset = [[AVURLAsset alloc] initWithURL:assetURL options:assetOptions];
self.imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:myAsset];
int duration = CMTimeGetSeconds([myAsset duration]);
for(int i = 0; i<duration; i++)
{
CGImageRef imgRef = [self.imageGenerator copyCGImageAtTime:CMTimeMake(i, duration) actualTime:NULL error:nil];
UIImage* thumbnail = [[UIImage alloc] initWithCGImage:imgRef scale:UIViewContentModeScaleAspectFit orientation:UIImageOrientationUp];
[thumbnailImages addObject:thumbnail];
}
I am using above code to get thumbnail images but the problem is if there is a 2 seconds video i am only getting 2 thumbnails but i want 20 thumbnails (10 thumbnail per second).
So, how to use CMTimeMake to get thumbnails for every .1 second
A video thumbnail is a still image that acts as the preview image for your video. It's kind of like a book cover. And, like a book cover, it should entice a potential viewer to want to see more. The term “thumbnail” originated with still images.
Code form reference site : Thumbnail image from Video
Objective - C
-(UIImage *)generateThumbImage : (NSString *)filepath
{
NSURL *url = [NSURL fileURLWithPath:filepath];
AVAsset *asset = [AVAsset assetWithURL:url];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
self.imageGenerator.appliesPreferredTrackTransform = YES;
CMTime time = [asset duration];
time.value = 0;
Float duration = CMTimeGetSeconds([myAsset duration]);
for(Float i = 0.0; i<duration; i=i+0.1)
{
CGImageRef imgRef = [self.imageGenerator copyCGImageAtTime:CMTimeMake(i, duration) actualTime:NULL error:nil];
UIImage* thumbnail = [[UIImage alloc] initWithCGImage:imgRef scale:UIViewContentModeScaleAspectFit orientation:UIImageOrientationUp];
[thumbnailImages addObject:thumbnail];
}
}
Swift
func generateThumbImage(url : NSURL) -> UIImage{
var asset : AVAsset = AVAsset.assetWithURL(url) as! AVAsset
var assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
var error : NSError? = nil
var time : CMTime = CMTimeMake(1, 30)
var img : CGImageRef = assetImgGenerate.copyCGImageAtTime(time, actualTime: nil, error: &error)
var frameImg : UIImage = UIImage(CGImage: img)!
return frameImg
}
@Kirit Modi solution in Swift 3 with some small changes:
func generateThumbImage(url : URL) -> UIImage?{
let asset = AVAsset(url: url)
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With