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

  • 相关阅读:
    weakself的另一种写法
    SDWebImage的使用说明
    关于AVPlayerItem对象的属性duration返回播放总时长的坑
    Xcode 问题
    循环引用问题
    使用Spring Animation的API创建动画
    iOS触摸事件处理
    OS开发中的事件处理(二)-事件传递,响应者链条
    python实现简单的百度云自动下载
    我眼中的K-近邻算法
  • 原文地址:https://www.cnblogs.com/Proteas/p/2456116.html
Copyright © 2011-2022 走看看