iOS 对视频抽帧。

这里有两种方法可以采用,

方法一:使用MPMoviePlayerController

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

moviePlayer.shouldAutoplay = NO; 
UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:time timeOption:MPMovieTimeOptionNearestKeyFrame];

 

优点:操作简单,省事

缺点:不够轻量,抓取图片耗时。

 

方法二:使用AVAssetImageGenerator

+ (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;

    return thumbnailImage;
}

 

posted @ 2014-12-19 14:45  一码一生  阅读(1993)  评论(0编辑  收藏  举报