I am using AVAssetImageGenerator for generating the thumbnail images for video play buttons.
When i pass server video url to generate image it is retuning null.
AVAsset *asset = [AVAsset assetWithURL:url];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
CMTime time = CMTimeMake(51,1);
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *thumbnail =[[UIImage alloc]initWithCGImage:imageRef];
CGImageRef cgref = [thumbnail CGImage];
CIImage *cim = [thumbnail CIImage];
if (cim == nil && cgref == NULL)
{
NSLog(#"no underlying data");
}
CGImageRelease(imageRef);
Always i am getting null data.rarely i am getting only one image.
How can i solve this problem.
You can try the following code. Its working for me
AVURLAsset *as = [[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *ima = [[AVAssetImageGenerator alloc] initWithAsset:as];
NSError *err = NULL;
CMTime time = CMTimeMake(0, 60);
CGImageRef imgRef = [ima copyCGImageAtTime:time actualTime:NULL error:&err];
[ima release];
UIImage *currentImg = [[UIImage alloc] initWithCGImage:imgRef];
And i got a reference is that the easiest way is to just set the appliesPreferredTrackTransform property on the image generator to YES, then it should automatically do the transformation for you.
Hope this helps !!!
Related
Hi i want to create a thumbnail from my video, is 2.46 minutes long and 68 Mb.
I used this code to create the thumbnail :
AVAsset *asset = [AVAsset assetWithURL:videoURL];
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;
The issue is the thumbnail is always nil, but if i reduce the video length to 15 secs it will create the thumbnail.
Can i create thumbnail for movie that more than 15 secs?
thanks
Mmmm this way to use AVAssetImageGenerator works:
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:videoURL options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
CGFloat videoLenght=((float)asset.duration.value)/((float)asset.duration.timescale);
CMTime thumbTime = CMTimeMakeWithSeconds(videoLenght,2.0);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
if (result != AVAssetImageGeneratorSucceeded) {
NSLog(#"couldn't generate thumbnail, error:%#", error);
}else{
UIImage *image=[self.class scaledImage:[UIImage imageWithCGImage:im]];
[self performSelectorOnMainThread:#selector(setVideoImage:) withObject:image waitUntilDone:NO];
}
};
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
(videoLenght,2.0) is the time where the snapshot takes place, in this case half of the video, I do not recommend you to use a fixed screenshot time because if the time is out of bounds the image will be nil.
I want to fetch video thumbnail from URL instead using MPMoviePlayerController and AVAssetImageGenerator. Because these have some issue. Issue I have already asked,
1st one is : AVAssetImageGenerator not working properly, I am unable to fetch thumbnail of video from URL
and 2nd is : https://stackoverflow.com/questions/32431311/mpmovieplayercontroller-not-fetching-thumbnail-of-video-from-url-properly
I have no idea how to do it. Any suggestion will be great. Thank in advance !!!
All you need is to put into comment below your URL and self.imageView.image = FrameImage;
Anyway.
NSString *str=#"https://scontent.cdninstagram.com/hphotos-xfa1/t50.2886-16/11719145_918467924880620_816495633_n.mp4";
NSURL *url=[[NSURL alloc]initWithString:str];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
NSError *error = NULL;
CMTime thumbTime = CMTimeMakeWithSeconds(0,30);
CGImageRef refImg = [generator copyCGImageAtTime:thumbTime actualTime:NULL error:&error];
if(error) {
NSLog(#"%#", [error localizedDescription]);
}
UIImage *FrameImage= [[UIImage alloc] initWithCGImage:refImg];
[self.imageView setImage:FrameImage];
Try this one.
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:#"[video URL]"] options:nil];
AVAssetImageGenerator *generateImg = [[AVAssetImageGenerator alloc] initWithAsset:asset];
NSError *error = NULL;
CMTime time = CMTimeMake(1, 65);
CGImageRef refImg = [generateImg copyCGImageAtTime:time actualTime:NULL error:&error];
NSLog(#"error==%#, Refimage==%#", error, refImg);
UIImage *FrameImage= [[UIImage alloc] initWithCGImage:refImg];
There are lot of example codes for getting thumbnail from a video url. But all of them I tried are NOT generating the image asynchronously. So, when it comes to multiple videos in a tableview, app freezes. Please help me to make this thumbnail asynchronously.
-(UIImage *)generateThumbnailIconForVideoFileWith:(NSURL *)contentURL WithSize:(CGSize)size
{
UIImage *theImage = nil;
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:contentURL options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.maximumSize=size;
generator.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
CMTime time = CMTimeMake(100,100); //change whatever you want here.
CGImageRef imgRef = [generator copyCGImageAtTime:time actualTime:NULL error:&err];
theImage = [[UIImage alloc] initWithCGImage:imgRef] ;
CGImageRelease(imgRef);
return theImage;
}
You can also use MPMovieplayerfor generating thumbnail
MPMoviePlayerController *player= [[MPMoviePlayerController alloc]initWithContentURL:Videourl];
self.imgshow.image = [player thumbnailImageAtTime:5.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
[player stop];
I have a video in gallery. on clicking it tries to open,but instead of that i want to show a thumbnail of video in uiimageview.
AVURLAsset *asset;
if ([url isFileURL]) {
asset = [[AVURLAsset alloc] initWithURL:url options:nil];
}
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
UIImage *finalThumpnailImage = [HPLUtility createThumpnailImageFromTheImage:thumb]; //Resized image.
CGImageRelease(image);
return finalThumpnailImage;
Iam using UIImagePicker to record video when i click the record button i want to capture the frame with sound and add it into one array .Please help me to accomplish the same thanks in advance .
try this sample code for record videos in frame
customphotoalbum
Edit
- (void)video:(NSString*)videoPath didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo {
NSLog(#"%#",contextInfo);
if (error) {
}else{
NSURL *videoURl = [NSURL fileURLWithPath:videoPath];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURl options:nil];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generate.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
CMTime time = CMTimeMake(1, 60);// heare you can set more then one image time
CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
self.strUploadVideoURL = videoPath;
self.strOriginalVideoURL = videoPath;
UIImage *img = [[UIImage alloc] initWithCGImage:imgRef];
// use this image to add in array.
}
}