美文网首页iOS常用功能和UI效果的实现
iOS截取指定时间的视频截图

iOS截取指定时间的视频截图

作者: AnthonyChen | 来源:发表于2017-03-26 21:49 被阅读477次

以前的项目中有一个类似微信短视频的功能,在视频播放前,会先显示视频对应的截图(一般为视频第一帧的画面),以下代码可以获取到视频文件中任意秒的截图

/**

*  截取指定时间的视频缩略图

*

*  @param timeBySecond 时间点

*/-(void)thumbnailImageRequest:(CGFloat )timeBySecond{

//创建URL

NSURL *url=[self getNetworkUrl];

//根据url创建AVURLAsset 

AVURLAsset *urlAsset=[AVURLAsset assetWithURL:url];

//根据AVURLAsset创建AVAssetImageGenerator

AVAssetImageGenerator *imageGenerator=[AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset];

/*截图

* requestTime:缩略图创建时间

* actualTime:缩略图实际生成的时间

*/NSError *error=nil;

CMTime time=CMTimeMakeWithSeconds(timeBySecond, 10);

//CMTime是表示电影时间信息的结构体,第一个参数表示是视频第几秒,第二个参数表示每秒帧数.(如果要活的某一秒的第几帧可以使用CMTimeMake方法)

CMTime actualTime;

CGImageRef cgImage= [imageGenerator copyCGImageAtTime:time actualTime:&actualTime error:&error];

if(error){

NSLog(@"截取视频缩略图时发生错误,错误信息:%@",error.localizedDescription);return;

}

CMTimeShow(actualTime);

UIImage *image=[UIImage imageWithCGImage:cgImage];//转化为UIImage

//获取到图片文件后,可以选择上传至服务器了

}

相关文章

网友评论

  • Chars:不要在主线程做,会卡的
    AnthonyChen:谢谢:blush:
  • 7eg:这个截图需要开线程去处理吗?
    远方的枫叶:@AnthonyChen 可以截取网络视频吗,还是只支持本地的视频
    AnthonyChen:我在使用的过程中没有开,应该也是可以正常使用的

本文标题:iOS截取指定时间的视频截图

本文链接:https://www.haomeiwen.com/subject/dstlottx.html