美文网首页
截取视频指定时间点的某一帧

截取视频指定时间点的某一帧

作者: ___吉 | 来源:发表于2019-11-15 10:36 被阅读0次

    截取单帧

    AVAsset *myAsset = <#An asset#>];
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:myAsset];
         
    Float64 durationSeconds = CMTimeGetSeconds([myAsset duration]);
    CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);
    NSError *error;
    CMTime actualTime;
         
    CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error];
         
    if (halfWayImage != NULL) {
      NSString *actualTimeString = (NSString *)CMTimeCopyDescription(NULL, actualTime);
      NSString *requestedTimeString = (NSString *)CMTimeCopyDescription(NULL, midpoint);
      NSLog(@"Got halfWayImage: Asked for %@, got %@", requestedTimeString, actualTimeString);
         
      // Do something interesting with the image.
      CGImageRelease(halfWayImage);
    }
    

    截取多帧

        AVAsset *myAsset = <#An asset#>];
        // Assume: @property (strong) AVAssetImageGenerator *imageGenerator;
        self.imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:myAsset];
        
        Float64 durationSeconds = CMTimeGetSeconds([myAsset duration]);
        CMTime firstThird = CMTimeMakeWithSeconds(durationSeconds/3.0, 600);
        CMTime secondThird = CMTimeMakeWithSeconds(durationSeconds*2.0/3.0, 600);
        CMTime end = CMTimeMakeWithSeconds(durationSeconds, 600);
        NSArray *times = @[NSValue valueWithCMTime:kCMTimeZero],
        [NSValue valueWithCMTime:firstThird], [NSValue valueWithCMTime:secondThird],
        [NSValue valueWithCMTime:end]];
        
        [imageGenerator generateCGImagesAsynchronouslyForTimes:times
                                             completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime,
                                                                 AVAssetImageGeneratorResult result, NSError *error) {
            
            NSString *requestedTimeString = (NSString *)
            CFBridgingRelease(CMTimeCopyDescription(NULL, requestedTime));
            NSString *actualTimeString = (NSString *)
            CFBridgingRelease(CMTimeCopyDescription(NULL, actualTime));
            NSLog(@"Requested: %@; actual %@", requestedTimeString, actualTimeString);
            
            if (result == AVAssetImageGeneratorSucceeded) {
                // Do something interesting with the image.
            }
            
            if (result == AVAssetImageGeneratorFailed) {
                NSLog(@"Failed with error: %@", [error localizedDescription]);
            }
            if (result == AVAssetImageGeneratorCancelled) {
                NSLog(@"Canceled");
            }
        }];
    
    引用参考:

    Apple文档

    相关文章

      网友评论

          本文标题:截取视频指定时间点的某一帧

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