Tim Reynaert
Tim Reynaert

Reputation: 175

MPMoviePlayerController generate thumbnail of local video file and store it

I'm making an app that downloads movies from the server and stores them localy in the NSDocumentDirectory.

This works fine.

I want to add a thumbnail generated from each movie in front of the name in each cell.

My problem:

How can I generate a thumbnail from a movie after it is downloaded (so instantly, without having to play the movie first)? I want to store the thumbnails with the same name of the movie as a jpg in the NSDocumentDirectory.

My guess

-download movie and store it in NSDocumentDirectory (works)

-somehow load the movie in the MPMoviePlayerController's memory (don't know how)

-when loaded in memory, generate thumbnail with thumbnailImageAtTime (MPMovieTimeOptionNearestKeyFrame) (should work)

-store it (should work)

If anyone could help me...

Thanks

Upvotes: 0

Views: 5291

Answers (3)

roberto.buratti
roberto.buratti

Reputation: 2497

Yes, using MPMoviePlayer works... but you must be sure that you do not have another movie player playing elsewhere in your app (even the UIWebView plug-in...) or you will get in trouble. I do this way:

UIImage *thumbnail = nil;
NSURL *url = [NSURL fileURLWithPath:pathname];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform = YES;
NSError *error = nil;
CMTime time = CMTimeMake(3, 1); // 3/1 = 3 second(s)
CGImageRef imgRef = [generator copyCGImageAtTime:time actualTime:nil error:&error];
if (error != nil)
    NSLog(@"%@: %@", self, error);
thumbnail = [[UIImage alloc] initWithCGImage:imgRef];
CGImageRelease(imgRef);

Hope this might help

Upvotes: 3

Tim Reynaert
Tim Reynaert

Reputation: 175

This is the code I use which should generate the thumbnail (added a big uiimageview for testing, works when I load a local image in it)

NSString *path;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"snijtechniekendir/videos"];
    path = [path stringByAppendingPathComponent:[videos objectAtIndex:indexPath.row]];

    NSURL *videoURL = [NSURL fileURLWithPath:path];

    NSLog(@"video url: %@", videoURL);

    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];

    UIImage *thumbnail = [player thumbnailImageAtTime:1 timeOption:MPMovieTimeOptionNearestKeyFrame];

    UIImageView *cellimage = [[UIImageView alloc] initWithFrame:CGRectMake(2, 2 , 400, 400)];

    [cell.contentView addSubview:cellimage];
    NSData *imgData = UIImagePNGRepresentation(thumbnail);
    NSLog(@"lenght of video thumb: %@", [imgData length]);
    [self.view addSubview:cellimage];
    [cellimage setImage:thumbnail];

This is the log for one video file

video url: file://localhost/Users/Home/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/78C165BB-75A9-46A2-A257-469F8652A665/Documents/snijtechniekendir/videos/snijtechniek%2520brunoise.mp4

lenght of video thumb: (null)

Upvotes: 1

Warif Akhand Rishi
Warif Akhand Rishi

Reputation: 24248

#import <MediaPlayer/MediaPlayer.h>   


-(UIImage*)getFirstFrameFromVideoFile:(NSString*)sourceFilePath {
NSURL *videoURL = [NSURL fileURLWithPath:sourceFilePath];

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];

UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
//Player autoplays audio on init
[player stop];
[player release];
return thumbnail;
}

Other tasks you know already.

Upvotes: 5

Related Questions