美文网首页
读取解析视频帧图片

读取解析视频帧图片

作者: 杨继磊 | 来源:发表于2021-11-25 17:02 被阅读0次
    #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");
            }
        }];
        
    }
    
    

    相关文章

      网友评论

          本文标题:读取解析视频帧图片

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