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

  • 相关阅读:
    事件总线demo
    软件架构分类(转载)
    ASP.NET MVC 使用 Datatables (2)
    ASP.NET MVC 使用 Datatables (1)
    查看win10的激活信息和版本号
    2016年工作计划
    通俗粗暴的事件委托理解
    matplotlib系列——条形图
    matplotlib系列——折线图
    使用pip安装python模块和包
  • 原文地址:https://www.cnblogs.com/Proteas/p/2456116.html
Copyright © 2011-2022 走看看