gpt4 book ai didi

ios - AVAssetImageGenerator 有时会从 2 个连续帧返回相同的图像

转载 作者:可可西里 更新时间:2023-11-01 03:26:25 25 4
gpt4 key购买 nike

我目前正在使用 AVAssetImageGenerator 从视频中提取每一帧,但有时它会连续两次返回几乎相同的图像(它们没有相同的“帧时间”)。有趣的是它总是每 5 帧发生一次(在我的测试视频中)。

Herehere是两个图像(在新选项卡中打开每个图像,然后切换选项卡以查看差异)。

这是我的代码:

//setting up generator & compositor
self.generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
generator.appliesPreferredTrackTransform = YES;
self.composition = [AVVideoComposition videoCompositionWithPropertiesOfAsset:asset];

NSTimeInterval duration = CMTimeGetSeconds(asset.duration);
NSTimeInterval frameDuration = CMTimeGetSeconds(composition.frameDuration);
CGFloat totalFrames = round(duration/frameDuration);

NSMutableArray * times = [NSMutableArray array];
for (int i=0; i<totalFrames; i++) {
NSValue * time = [NSValue valueWithCMTime:CMTimeMakeWithSeconds(i*frameDuration, composition.frameDuration.timescale)];
[times addObject:time];
}

AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
// If actualTime is not equal to requestedTime image is ignored
if(CMTimeCompare(actualTime, requestedTime) == 0) {
if (result == AVAssetImageGeneratorSucceeded) {
NSLog(@"%.02f %.02f", CMTimeGetSeconds(requestedTime), CMTimeGetSeconds(actualTime));
// Each log have differents actualTimes.
// frame extraction is here...
}
}
};

generator.requestedTimeToleranceBefore = kCMTimeZero;
generator.requestedTimeToleranceAfter = kCMTimeZero;
[generator generateCGImagesAsynchronouslyForTimes:times completionHandler:handler];

知道它可能来自哪里吗?

最佳答案

请查看 AVAssetImageGenerator 的以下属性。您应该为这两个属性设置 kCMTimeZero 以获得准确的帧。

/* The actual time of the generated images will be within the range [requestedTime-toleranceBefore, requestedTime+toleranceAfter] and may differ from the requested time for efficiency.
Pass kCMTimeZero for both toleranceBefore and toleranceAfter to request frame-accurate image generation; this may incur additional decoding delay.
Default is kCMTimePositiveInfinity. */
@property (nonatomic) CMTime requestedTimeToleranceBefore NS_AVAILABLE(10_7, 5_0);
@property (nonatomic) CMTime requestedTimeToleranceAfter NS_AVAILABLE(10_7, 5_0);

在我为这两个属性设置 kCMTimeZero 之前,我得到了一些与您所经历的不同请求时间相同的图像。只需尝试以下代码。

self.imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:myAsset];
self.imageGenerator.requestedTimeToleranceBefore = kCMTimeZero;
self.imageGenerator.requestedTimeToleranceAfter = kCMTimeZero;

关于ios - AVAssetImageGenerator 有时会从 2 个连续帧返回相同的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15245781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com