how to display the thumbnail image - ios

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"]]];

Related

How to capture still image from custom video player screen in iOS?

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];

How do I get NSArray of images from a video? Vice-versa is possible

It is possible that I can play video from NSArray of images. But how can I get NSArray of images from a video?
You can use this code to make image from Video and later you can save those image in a array
UIImage *singleFrameImage = [self.theMoviePlayer
thumbnailImageAtTime:i
timeOption:MPMovieTimeOptionExact];
To get a image from your video use this sample.
First import AVFoundation
#import <AVFoundation/AVFoundation.h>
Create the NSURL from your video.
NSString *videoUrl = #"http://download.wavetlan.com/SVV/Media/HTTP/MP4/ConvertedFiles/Media-Convert/Unsupported/test7.mp4";
NSURL *url = [NSURL URLWithString:videoUrl];
Prepare the asset.
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generate.appliesPreferredTrackTransform = YES;
Select the time to cut: e.g. In second 2 for 1 second.
CMTime time = CMTimeMake(2, 1);
Get the ImageRefference and the Thumbnail.
NSError *error = nil;
CGImageRef imageRef = [generate copyCGImageAtTime:time actualTime:nil error:&error];
UIImage *thumbnail = [[UIImage alloc] initWithCGImage:imageRef];
Set your image to some UIImageView .
[self.imageView setImage:thumbnail];
The Sample shows this image

Get thumbnail of *.mov file at 30 sec.?

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];

Getting thumbnail from a video url asynchronously - iOS

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];

Capturing images while record button clicked in ios simultaneously

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.
}
}

Resources