AVFoundation之截取视频中某一帧图片

测试系统:ios10、ios11

1.使用asset方式获取

- (UIImage*) getVideoPreViewImage{

AVURLAsset*asset = [[AVURLAssetalloc] initWithURL:videoPath options:nil];

AVAssetImageGenerator*gen = [[AVAssetImageGeneratoralloc] initWithAsset:asset];

gen.appliesPreferredTrackTransform =YES;

long long second = asset.duration.value / asset.duration.timescale; // 获取视频总时长,单位秒

CMTime time =CMTimeMakeWithSeconds(second/2,60);

NSError*error =nil;

CMTime actualTime;

CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];

UIImage*img = [[UIImage alloc] initWithCGImage:image];

CGImageRelease(image);

return img;

}

分析:

CMTime 是一个用来描述视频时间的结构体。

他有两个构造函数: * CMTimeMake * CMTimeMakeWithSeconds

这两个的区别是 * CMTimeMake(a,b) a当前第几帧, b每秒钟多少帧.当前播放时间a/b * CMTimeMakeWithSeconds(a,b) a当前时间,b每秒钟多少帧.

2.使用movieplayer方式获取

MPMoviePlayerController*moviePlayer =      [[MPMoviePlayerControlleralloc] initWithContentURL:videoURL];

  moviePlayer.shouldAutoplay =NO;

 [moviePlayer Play];

 UIImage*thumbnail =  [moviePlayer thumbnailImageAtTime:time    timeOption:MPMovieTimeOptionNearestKeyFrame];

 [MoviePlayer Pause];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容