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

你可能感兴趣的:(AVFoundation之截取视频中某一帧图片)