zoukankan      html  css  js  c++  java
  • [转]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;
    }

  • 相关阅读:
    html里特殊标签,试下就知道是什么了
    Eclipse 中文乱码问题
    PHP中的MYSQL常用函数总结
    PHP中$_SERVER的详细用法
    JQUERY获取radio,checkbox,select值
    jquery基础教程七 选择器(selectors 的xpath语法应用)
    php培训班的课程表 可自学参考
    php中trim()的特殊用法
    80后
    JQuery Attributes 方法说明
  • 原文地址:https://www.cnblogs.com/Proteas/p/2456116.html
Copyright © 2011-2022 走看看