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;
    }

  • 相关阅读:
    霍夫直线检测进行复杂环境的树干提取
    matlab工具箱之人眼检测+meanshift跟踪算法--人眼跟踪
    deep learning 练习 牛顿法完成逻辑回归
    deep learning 练习 多变量线性回归
    deep learning 练习1 线性回归练习
    关于移动端键盘弹出
    关于Redux
    docker registry的CI规划
    建立自己的私有docker(ssl&login auth)
    逻辑编程
  • 原文地址:https://www.cnblogs.com/Proteas/p/2456116.html
Copyright © 2011-2022 走看看