I just create my own custom video player using AVFoundation library's AVPlayer* myPlayer. Now I want to capture images for bookmarks of video from running playing video on UIButton click action. On click UIButton it take snapshot of player screen and store in document directory.
Please suggest me how to do it?
Thank you in advance.
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
Reference Link :
Create a thumbnail or image of an AVPlayer at current time
AVAsset *asset = [AVAsset assetWithURL:videoURL];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform=TRUE;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:player.currentTime
actualTime:NULL error:NULL];
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 am develping on app in which i have many videos, when videos are loaded from server to application it shows black screen while loaded from server. so i write following code to get thumbnail of video and set it to my UIView but i want to take thumbnail image of video after 30 sec. screen.
when i need to change in following code to get that.
NSURL *vidURL = [[NSURL alloc]initWithString:objAlbum.strVideoURL];
NSLog(#"URL IS : %#",vidURL);
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:vidURL];
UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
[player stop];
[cell2.playVWimage setImage:thumbnail];
Have you tried this,
AVAsset *myAsset = [[AVURLAsset alloc] initWithURL:videoUrl options:nil];
AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:myAsset];
CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:TIME actualTime:&actualTime error:&error];
UIImage *image;
image = [[UIImage alloc] initWithCGImage:halfWayImage scale:2.0 orientation:UIImageOrientationUp];
How to display the Thumbnail image for the video before playing. This is Simple question ,I am new in this field can any one help me to solve this problem
NSString *str11 = [NSString stringWithFormat:#"https://www.youtube.com/embed/XGSy3_Czz8k?autoplay=1"];
NSURL *url1 = [NSURL URLWithString:str11];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url1] ;
[player play];
AVAsset *asset = [AVAsset assetWithURL:url1];
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];
NSLog(#"thumbmail %#",thumbnail);
If there is no requirement to use MPMoviewPlayer then you can use UIWebView. There you will have preview thumbnail loaded automatically.
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"https://www.youtube.com/embed/XGSy3_Czz8k?autoplay=1"]]];
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.
}
}
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 !!!