• [转]iOS 应用中对视频进行抽帧的方法


    You can do this in one of two ways. The first way is to use the MPMoviePlayerController to grab the thumbnail:

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

    This works, but MPMoviePlayerController is not a particularly lightweight object and not particularly fast grabbing thumbnails.

    The preferred way is to use the new AVAssetImageGenerator in AVFoundation. This is fast, lightweight and more flexible than the old way. Here's a helper method that will return an autoreleased image from the video.


    + (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time {
       
    AVURLAsset *asset = [[[AVURLAsset alloc] initWithURL:videoURL options:nil] autorelease];
       
    NSParameterAssert(asset);
       
    AVAssetImageGenerator *assetImageGenerator = [[[AVAssetImageGenerator alloc] initWithAsset:asset] autorelease];
        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] autorelease] : nil;

       
    return thumbnailImage;
    }

  • 相关阅读:
    Day1-CSS-下拉菜单
    scau 1138 代码等式
    SCAU-1076 K尾相等数
    勾股数专题-SCAU-1079 三角形-18203 神奇的勾股数(原创)
    SCAU-1144 数星星-HDU-1166-树状数组的应用
    NodeJs Fs模块
    Node核心模块
    flutter环境配置
    纯CSS制作图形效果
    CSS3选择器
  • 原文地址:https://www.cnblogs.com/Proteas/p/2456116.html
Copyright © 2020-2023  润新知