#pragma mark - 读取解析视频帧图片
-(void)analysisVideoFrames{
AVURLAsset *videoAsset = [[AVURLAsset alloc]initWithURL:self.videoUrl options:nil];
//获取视频总长度 = 总帧数 / 每秒的帧数
long videoSumTime = videoAsset.duration.value / videoAsset.duration.timescale;
//创建AVAssetImageGenerator对象
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc]initWithAsset:videoAsset];
generator.maximumSize = self.bottomView.frame.size;
generator.appliesPreferredTrackTransform = YES;
generator.requestedTimeToleranceBefore = kCMTimeZero;
generator.requestedTimeToleranceAfter = kCMTimeZero;
//添加需要帧数的时间集合
self.framesArray = [NSMutableArray array];
for (NSInteger index = 0; index < videoSumTime; index ++) {
CMTime time = CMTimeMake(index * videoAsset.duration.timescale, videoAsset.duration.timescale);
NSValue *value = [NSValue valueWithCMTime:time];
[self.framesArray addObject:value];
}
__block long count = 0 ;
__weak typeof(self)weakSelf = self;
__block UIImage *showImage = [[UIImage alloc] init];
__block CGFloat showImageViewWitd = (self.bottomView.frame.size.width - self.leftView.frame.size.width * 2)/videoSumTime;
[generator generateCGImagesAsynchronouslyForTimes:self.framesArray completionHandler:^(CMTime requestedTime, CGImageRef _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) {
if (result == AVAssetImageGeneratorSucceeded) {
showImage = [UIImage imageWithCGImage:image];
dispatch_async(dispatch_get_main_queue(), ^{
UIImageView *thumImgView = [[UIImageView alloc]initWithFrame:CGRectMake( 20 + count * showImageViewWitd , 0, showImageViewWitd, 40)];
thumImgView.image = showImage;
[weakSelf.showImageViewBgView addSubview:thumImgView];
count++;
}) ;
}
if (result == AVAssetImageGeneratorFailed) {
NSLog(@"Failed with error: %@", [error localizedDescription]);
}
if (result == AVAssetImageGeneratorCancelled) {
NSLog(@"AVAssetImageGeneratorCancelled");
}
}];
}
读取解析视频帧图片
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前端根据后端数据生成可显示图像是一个比较常见的功能,比如验证码,动态海报。后端传数据到前端,前端解析成校可视化验数...
- 方法一:通过imageio库和skimage库 安装环境:pip install imageiopip insta...
- 本想简单写个demo程序,读取摄像头的rtsp视频流,将最新帧保存为jpg图片传给AI程序进行识别,结果网上针对l...