Alex Wayne
Alex Wayne

Reputation: 187034

Getting iPhone video thumbnails

How do I get a thumbnail of a video imported from the camera roll, or the camera itself?

This has been asked before, and has been answered. However, the answers kind of suck for me.

This thread iphone sdk > 3.0 . Video Thumbnail? has some options that boil down to:

  1. Crawl some filesystem directory for a JPG with the latest modification date that should correspond to the video you just picked. This is extremely messy, and involves rooting around in directories Apple would probably not really want me doing.
  2. Use ffmpeg. But this is so general that I cannot seem to figure out the steps that it would take to import ffmpeg into my project and to actually call it to extract images.

Is there really no other way? This seems like a HUGE oversight in the SDK to me. I mean the video picker has thumbnails in it, so Apple must be doing something to generate those, yet does not allow us to?

Upvotes: 3

Views: 6471

Answers (3)

E-Madd
E-Madd

Reputation: 4572

Best method I've found... MPMoviePlayerController thumbnailImageAtTime:timeOption

Nevermind this... see first comment below. That's the answer.

Upvotes: 3

jim
jim

Reputation: 141

     -(void)testGenerateThumbNailDataWithVideo {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"IMG_0106" ofType:@"MOV"];
 NSURL *url = [NSURL fileURLWithPath:path];
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url 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];
    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;
    }
    [currentImg release]; 
}

Upvotes: 12

michelle
michelle

Reputation: 11

We use ffmpeg, you can explore our site for hints on how to do it, eventually I want to put up a tutorial.

But right now I'm more concentrated on getting ffmpeg to play movies. Understand once you have that code the code to generate a thumbnail is just a subset of that. http://sol3.typepad.com/tagalong_developer_journa/

This tutorial here, has helped us and maybe the majority of developers using ffmpeg to get started. dranger.com/ffmpeg/ "

Finally,

Apple probably would maybe not have any problems with using the thumbnail generated from the video camera, I don't think its in a private folder however that is only created by the camera and not for videos picked from the image picker.

Upvotes: 1

Related Questions