Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Thumbnail for Video in iOS

I have an application that I am developing for the iPhone. What it does is, it captures the video from the camera and stores the video file onto the File System.

I need to create a Thumbnail Image of this video file before I save the Image to the File System. My motive is to show a list of thumbnails of the created video so that the user can select a specific thumbnail to play the desired file.

Could someone please advise on how I can create a Thumbnail image of a video file that has been captured by the Camera.

Also, can you please advise if I can create a Thumbnail of an existing video file using iOS SDK.

like image 536
Abishek Avatar asked May 11 '11 11:05

Abishek


People also ask

How do I change the thumbnail of a video on Apple?

Question: Q: How to change the Thumbnail of a video in Photos app 6.0. To change the thumbnail of a video you have to edit the video then, in the timeline at the Botton click on the frame you would like to use as the thumbnail and choose the option "Make Poster Frame", then click done.


2 Answers

A better solution actually is to use the AVFoundation framework to do this. It bypasses the need to construct an MPMoviePlayerController which causes the problem that the Iris of the camera remains closed if used in conjuction with the UIImagePickerController (at least that's what I experienced).

The code I use:

+ (UIImage *)thumbnailFromVideoAtURL:(NSURL *)contentURL {
    UIImage *theImage = nil;
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:contentURL options:nil];
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    generator.appliesPreferredTrackTransform = YES;
    NSError *err = NULL;
    CMTime time = CMTimeMake(1, 60);
    CGImageRef imgRef = [generator copyCGImageAtTime:time actualTime:NULL error:&err];

    theImage = [[[UIImage alloc] initWithCGImage:imgRef] autorelease];

    CGImageRelease(imgRef);
    [asset release];
    [generator release];

    return theImage;
}
like image 187
Werner Altewischer Avatar answered Oct 05 '22 11:10

Werner Altewischer


Try this (it doesn't actually show the movie player):

+ (UIImage *)imageFromMovie:(NSURL *)movieURL atTime:(NSTimeInterval)time {
  // set up the movie player
  MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] 
    initWithContentURL:movieURL];
  mp.shouldAutoplay = NO;
  mp.initialPlaybackTime = time;
  mp.currentPlaybackTime = time;
  // get the thumbnail
  UIImage *thumbnail = [mp thumbnailImageAtTime:time 
                           timeOption:MPMovieTimeOptionNearestKeyFrame];
  // clean up the movie player
  [mp stop];
  [mp release];
  return(thumbnail);
}

It's supposed to be a synchronous call, so it might block the main thread some, but seems to be running pretty instantly for me when I use a time at the beginning of the movie. If you're doing this a lot, you can add it as a category on UIImage, which is what I did.

I see from your question that you want to do this before the movie is saved, and I guess it might not work without a file url. However, if you're using the UIImagePickerController for camera capture, you can pass this function the URL returned in the info dictionary of imagePickerController:didFinishPickingMediaWithInfo: with the key UIImagePickerControllerMediaURL.

like image 31
Jesse Crossen Avatar answered Oct 05 '22 13:10

Jesse Crossen