Grabbing the first frame of a video from UIImagePickerController? - iphone

I'm trying to get the first frame from the selected video in a UIImagePickerController to show in a UIImageView, but I do not know if it's possible. If it is, how would I do it?

You can do this in one of two ways. The first way is to use the MPMoviePlayerController to grab the thumbnail:
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:videoURL];
moviePlayer.shouldAutoplay = NO;
UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:time
timeOption:MPMovieTimeOptionNearestKeyFrame];
This works, but MPMoviePlayerController is not a particularly lightweight object and not particularly fast grabbing thumbnails.
The preferred way is to use the new AVAssetImageGenerator in AVFoundation. This is fast, lightweight and more flexible than the old way. Here's a helper method that will return an autoreleased image from the video.
+ (UIImage *)thumbnailImageForVideo:(NSURL *)videoURL
atTime:(NSTimeInterval)time
{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
NSParameterAssert(asset);
AVAssetImageGenerator *assetIG =
[[AVAssetImageGenerator alloc] initWithAsset:asset];
assetIG.appliesPreferredTrackTransform = YES;
assetIG.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
CGImageRef thumbnailImageRef = NULL;
CFTimeInterval thumbnailImageTime = time;
NSError *igError = nil;
thumbnailImageRef =
[assetIG copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)
actualTime:NULL
error:&igError];
if (!thumbnailImageRef)
NSLog(#"thumbnailImageGenerationError %#", igError );
UIImage *thumbnailImage = thumbnailImageRef
? [[UIImage alloc] initWithCGImage:thumbnailImageRef]
: nil;
return thumbnailImage;
}
Asynchronous usage
- (void)thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time completion:(void (^)(UIImage *)) completion
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
NSParameterAssert(asset);
AVAssetImageGenerator *assetIG =
[[AVAssetImageGenerator alloc] initWithAsset:asset];
assetIG.appliesPreferredTrackTransform = YES;
assetIG.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
CGImageRef thumbnailImageRef = NULL;
CFTimeInterval thumbnailImageTime = time;
NSError *igError = nil;
thumbnailImageRef =
[assetIG copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)
actualTime:NULL
error:&igError];
if (!thumbnailImageRef)
NSLog(#"thumbnailImageGenerationError %#", igError );
UIImage *thumbnailImage = thumbnailImageRef
? [[UIImage alloc] initWithCGImage:thumbnailImageRef]
: nil;
dispatch_async(dispatch_get_main_queue(), ^{
completion(thumbnailImage);
});
});
}

Related

Thumbnail for youtube video

I am trying to create a thumbnail for youtube video,but me getting below error:-
Error Domain=AVFoundationErrorDomain Code=-11850 "Operation Stopped" UserInfo=0xa07ac00 NSLocalizedDescription=Operation Stopped, NSUnderlyingError=0xa07e580 "The operation couldn’t be completed. (OSStatus error -12939.)", NSLocalizedFailureReason=The server is not correctly configured.
Please help me out to create a thumbnail of youtube video from youtube link.
The Code am using to create thumbnail is as follows:-
NSURL *url = [NSURL URLWithString:strVideoURL];
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
CMTime thumbTime = CMTimeMakeWithSeconds(0,30);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error)
{
if (result != AVAssetImageGeneratorSucceeded) {
NSLog(#"couldn't generate thumbnail, error:%#", error);
}
// TODO Do something with the image
NSLog(#"CGImage---- %#", im);
UIImage *thumbnail=[UIImage imageWithCGImage:im];
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(30, 50, 120, 120)];
imageView.backgroundColor=[UIColor redColor];
imageView.image=thumbnail;
[image addSubview:imageView];
UIView *view=(UIView*)[self.view viewWithTag:10];
[view removeFromSuperview];
};
CGSize maxSize = CGSizeMake(128, 128);
generator.maximumSize = maxSize;
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
Although your question doesn't have any source code of yours so i have no idea where have you gone wrong. Try this, its working for me.
#define YOUTUBE_URL #"https://gdata.youtube.com/feeds/api/videos?v=2&author=%#&orderby=published"
NSString *userName = #"ipl";
_url = [NSURL URLWithString:[NSString stringWithFormat:YOUTUBE_URL,userName]];
-(void) fetchYoutubeThumbnails {
NSURLRequest *_request = [[NSURLRequest alloc] initWithURL: _url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:_request delegate:self];
[connection start];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[recievedData appendData:data]; //recievedData is a class variable of type NSMutableData
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *xmlString = [[NSString alloc] initWithData:recievedData encoding:NSUTF8StringEncoding];
NSDictionary *result = [XMLReader dictionaryForXMLString:xmlString error:Nil];
if(result.count > 0) {
NSMutableArray *items = [[result objectForKey:#"feed"] objectForKey:#"entry"];
for(int i = items.count -1; i >= 0; i--) {
NSDictionary *item = [items objectAtIndex:i];
NSString *videoThumbnaulUrl = [[[[item valueForKey:#"media:group"] valueForKey:#"media:thumbnail"] objectAtIndex:0] valueForKey:#"url"];
// Use this url to get the thumbnail.
}
}

Load thumbnail image from video saved at NSDocumentDirectory

I use the following code to get an image from a video at given path.
- (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time {
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
NSParameterAssert(asset);
AVAssetImageGenerator *assetImageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
assetImageGenerator.appliesPreferredTrackTransform = YES;
assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
CGImageRef thumbnailImageRef = NULL;
CFTimeInterval thumbnailImageTime = time;
NSError *thumbnailImageGenerationError = nil;
thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60) actualTime:NULL error:&thumbnailImageGenerationError];
if (!thumbnailImageRef)
NSLog(#"thumbnailImageGenerationError %#", thumbnailImageGenerationError);
UIImage *thumbnailImage = thumbnailImageRef ? [[UIImage alloc] initWithCGImage:thumbnailImageRef] : nil;
previewImage = thumbnailImage;
return thumbnailImage;
}
However this is not working for a video saved in a NSDocumentDirectory. Cant we access the NSDocumentDIrectory using NSURL? If not any alternative. The main idea is to show the thumbnail of an image saved in document directory and later allow it to upload to sever. This can be a temp directory as well.
Worked when you use NSURL *outputURL = [[NSURL alloc] initFileURLWithPath: path];

Can AVFoundation play Audio and get Details of it On the IOS 5.0 Simulator?

Can the Above be Done using only AVFoundation.framework?
i Tried doing a few experiment but i can't really get it to work
(sorry newbie here please do tell me where to put the codes at Example: ViewController or the MainView)
These are my code,(UI Components are inside as i'm do not want use interface builder)
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSError *error;
buttonPlay = [[UILabel alloc] initWithFrame:CGRectMake(0, 350, 100, 50)];
buttonPlay.text = #"Play";
// Get the file path to the song to play.
NSString* path;
NSURL* url;
path = [[NSBundle mainBundle] pathForResource:#"Bring_Me_To_Life.mp3" ofType:nil];
url = [NSURL fileURLWithPath:path];
NSData *_objectData = [NSData dataWithContentsOfURL:url];
//Initialize the AVAudioPlayer.
audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
// Preloads the buffer and prepares the audio for playing.
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = 1.0f;
[audioPlayer prepareToPlay];
if (audioPlayer == nil)
NSLog(#"%#", [error description]);
else
[audioPlayer play];
[self addSubview:buttonPlay];
}
return self;
}
-(void)touchesBegan:(CGPoint)location{
if(CGRectContainsPoint(buttonPlay.frame, location)){
NSLog(#"Dec from Chl %f",[audioPlayer peakPowerForChannel:0]);
NSLog(#"Length of Clip %f",[audioPlayer duration]);
}
}
try this
audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];

Access device music files from iPhone app programmatically

I want to access music files which are available on the iPhone and get it listed (or) get the file into my iPhone application some delegats and start playing it. Is it possible to do it ? Similar to how we access images from device photo album using UIImagePickerController delegate methods.
Thank you!
You can reference MPMediaPickerController class. It functions same as UIImagePickerController class.
-(void)loadDeviceMusic{
MPMediaQuery *everything = [[MPMediaQuery alloc] init];
[everything addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithBool:NO] forProperty:MPMediaItemPropertyIsCloudItem]];
NSArray *itemsFromGenericQuery = [everything items];
for (MPMediaItem *song in itemsFromGenericQuery) {
NSURL *assetURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
AVAsset *asset = [AVAsset assetWithURL:assetURL];
NSLog(#"SONGS URL=%#",assetURL);
if ([asset hasProtectedContent] == NO) {
MP3ObjectsClass *objMp3=[[MP3ObjectsClass alloc] init];
objMp3.mp3Url=[song valueForProperty:MPMediaItemPropertyAssetURL];
objMp3.mp3Duration=[song valueForProperty: MPMediaItemPropertyPlaybackDuration];
if([song valueForProperty: MPMediaItemPropertyTitle]){
objMp3.mp3Name=[song valueForProperty: MPMediaItemPropertyTitle];
}else{
objMp3.mp3Name=#"Unknown";
}
if([song valueForProperty: MPMediaItemPropertyArtist]){
objMp3.mp3ArtistName=[song valueForProperty: MPMediaItemPropertyArtist];
}else{
objMp3.mp3ArtistName=#"Unknown";
}
if([song valueForProperty: MPMediaItemPropertyAlbumTitle]){
objMp3.mp3AlbumTitle=[song valueForProperty: MPMediaItemPropertyAlbumTitle];
}else{
objMp3.mp3AlbumTitle=#"Unknown";
}
UIImage *mp3Image=[self getAlbumnArtWorkImage:assetURL];
if(mp3Image){
objMp3.mp3Image=mp3Image;
}else{
objMp3.mp3Image=[UIImage imageNamed:#"DefaultImage"];
}
[mp3Array addObject:objMp3];
}
}
}
-(UIImage *)getAlbumnArtWorkImage :(NSURL *)mp3Url{
AVAsset *asset = [AVURLAsset URLAssetWithURL:mp3Url options:nil];
UIImage *img = nil;
for (NSString *format in [asset availableMetadataFormats]) {
for (AVMetadataItem *item in [asset metadataForFormat:format]) {
if ([[item commonKey] isEqualToString:#"artwork"]) {
img = [UIImage imageWithData:[item.value copyWithZone:nil]];
}
}
}
return img;
}
*** MP3ObjectsClass is a NSObject class. Using above function you can access device music files from iPhone.
First import the AVFoundation/AVFoundation.h framework.
#import <AVFoundation/AVFoundation.h>
-(void)pickAudioFiles
{
MPMediaPickerController *soundPicker=[[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];
soundPicker.delegate=self;
soundPicker.allowsPickingMultipleItems=NO;
[self presentViewController:soundPicker animated:YES completion:nil];
}
-(void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
MPMediaItem *item = [[mediaItemCollection items] objectAtIndex:0];
NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
[mediaPicker dismissViewControllerAnimated:YES completion:nil];
AVPlayerItem *playerItem=[AVPlayerItem playerItemWithURL:url];
AVPlayer *player=[[AVPlayer alloc] initWithPlayerItem:playerItem];
AVPlayerLayer *playerLayer=[AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame=CGRectMake(0, 0, 10, 10);
[self.view.layer addSublayer:playerLayer];
}
and play with [player play];
If you want to use AVAudioPlayer then import AudioToolbox/AudioToolbox.h

Thumbnail image of video

I am selecting video clip from library. And i want to create thumbnail image of it. I have applied this code. But the image appeared rotated. I want its original view.
- (UIImage*)testGenerateThumbNailDataWithVideo {
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:appDelegate.videoURL options:nil];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
NSError *err = NULL;
CMTime time = CMTimeMake(1, 60);
CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
[generate release];
NSLog(#"err==%#, imageRef==%#", err, imgRef);
UIImage *currentImg = [[[UIImage alloc] initWithCGImage:imgRef] autorelease];
static BOOL flag = YES;
if (flag) {
NSData *tmpData = UIImageJPEGRepresentation(currentImg, 0.8);
NSString *path = [NSString stringWithFormat:#"%#thumbNail.png", NSTemporaryDirectory()];
BOOL ret = [tmpData writeToFile:path atomically:YES];
NSLog(#"write to path=%#, flag=%d", path, ret);
flag = NO;
}
return currentImg;
}
Try using AVAssetImageGenerator instead. Apple discusses using AVAssetImageGenerator to create thumbnails here. Here is sample code, which grabs a single thumbnail image. You will need to include the AVFoundation framework. And also add CoreMedia framework
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidPath 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];
CGImageRelease(image);
[gen release];
One more solution is
-(void)generateImage
{
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:self.url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
[asset release];
CMTime thumbTime = CMTimeMakeWithSeconds(0,30);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
if (result != AVAssetImageGeneratorSucceeded) {
NSLog(#"couldn't generate thumbnail, error:%#", error);
}
[button setImage:[UIImage imageWithCGImage:im] forState:UIControlStateNormal];
thumbImg=[[UIImage imageWithCGImage:im] retain];
[generator release];
};
CGSize maxSize = CGSizeMake(320, 180);
generator.maximumSize = maxSize;
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
}
Or
ALAsset
display image from URL retrieved from ALAsset in iPhone
Swift 5:
func previewImageForLocalVideo(at url: URL) -> 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.copyCGImage(at: time, actualTime: nil)
return UIImage(cgImage: imageRef)
} catch let error as NSError {
print("Image generation failed with error \(error)")
return nil
}
}
This will solve the rotation issue
generate.appliesPreferredTrackTransform = YES