Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking thumbnail image from video always returns null

I am trying to get the first thumbnail from a video.

I tried the following:

ONE

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:_moviePath] 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);
NSLog(@"the error is %@: image is %@: url is %@: ",error,_mainThumbnail,[NSURL fileURLWithPath:_moviePath] );

However, my log is:

the error is (null): image is (null): 
url is file:///private/var/mobile/Applications/D1293BDC-EA7E-4AC7-AD3C-1BA3548F37D6/tmp/trim.6F0C4631-E5E8-43CD-BF32-9B8F09D4ACF1.MOV: 

TWO

AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:_moviePath] 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);
        }
        _mainThumbnail.image = [UIImage imageWithCGImage:im];
        NSLog(@"thumbnails %@ ",_mainThumbnail.image);
    };

    CGSize maxSize = CGSizeMake(320, 180);
    generator.maximumSize = maxSize;
    [generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];

The log thumnails null.

Why is the image null? I looked in other forums, and they suggested to use fileURLWithPath -which I am already using.

I am using ios7

like image 821
Dejell Avatar asked Oct 06 '13 18:10

Dejell


2 Answers

You can try this method. It takes the first frame of the video at the given URL and returns it as UIImage.

- (UIImage *)thumbnailFromVideoAtURL:(NSURL *)url
{
    AVAsset *asset = [AVAsset assetWithURL:url];

    //  Get thumbnail at the very start of the video
    CMTime thumbnailTime = [asset duration];
    thumbnailTime.value = 0;

    //  Get image from the video at the given time
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];

    CGImageRef imageRef = [imageGenerator copyCGImageAtTime:thumbnailTime actualTime:NULL error:NULL];
    UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    return thumbnail;
}

The method does not check for errors as my code using it is OK with just nil returned if anything fails.


Edit: Clarification of CMTime values

CMTime has

  • timescale - think about this as a FPS (frames per second) of the video (25 FPS -> 1s of the video has 25 video frames)
  • value - identification of concrete frame. In the code above, this says which frame you want to take thumbnail of.

For calculation of frame at exact time, you have to make this calculation:

value = timescale * seconds_to_skip

which is the equivalent to

"Frame of the video to take" = "FPS" * "Number of seconds to skip"

If you want to take for example 1st frame of 2nd second of the video, you want in fact to skip 1st second of the video. So you would use:

CMTime thumbnailTime = [asset duration];
thumbnailTime.value = thumbnailTime.timescale * 1;

Another look: you want to skip first 1s of the video and than take a thumbnail. I.e. on 25 FPS, you want to skip 25 frames. So CMTime.value = 25 (skip 25 frames).

like image 57
Lukas Kukacka Avatar answered Sep 22 '22 04:09

Lukas Kukacka


Just in case someone made the same mistake as I did:

Check out if the URL is generated using appropriate method. You shouldn't get counfused for local and web URLs of the file.

In case of local video file, use below code:

NSURL *movieURL = [NSURL fileURLWithPath:_moviePath];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:movieURL options:nil];

In case of video file from internet, use below code:

NSURL *movieURL = [NSURL URLWithString:_moviePath];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:movieURL options:nil];
like image 33
Shamsiddin Saidov Avatar answered Sep 26 '22 04:09

Shamsiddin Saidov