菜鸟教程小白 发表于 2022-12-11 20:11:35

ios - AVAssetImageGenerator.generateCGImagesAsynchronously 的帧不正确


                                            <p><p>根据文档,<code>generateCGImagesAsynchronously</code> 接收 <code>NSValue</code> 数组,并从视频和给定时间生成帧,并将其作为回调返回。</p>

<p>我使用以下代码生成一个值列表</p>

<pre><code>    var values : = []
    let frameDuration = CMTimeMake(1, timeScale)
    for i in 0..&lt;duration{
      let lastFrameTime = CMTimeMake(Int64(i), timeScale)
      let presentationTime = (i == 0) ? lastFrameTime : CMTimeAdd(lastFrameTime, frameDuration)

      values.append(NSValue(time: presentationTime))

      //Next two lines of codes are just to cross check the output
      let image = try! imageGenerator.copyCGImage(at: presentationTime, actualTime: nil)
      let imageUrl = FileManagerUtil.getTempFileName(parentFolder: FrameExtractor.EXTRACTED_IMAGE, fileNameWithExtension: &#34;\(Constants.FRAME_SUFFIX)\(i)\(&#34;.jpeg&#34;)&#34;)
    }
</code></pre>

<p>正如您在上面的代码中看到的,我使用同步方法交叉检查了结果,我可以确认 <code>values</code> 数组持有正确的时间引用。</p>

<p>但是当相同的数组被传递给 <code>generateImageAsynchronously</code> 方法时,对于不同的时间戳,我会重复 10 次相同的帧。也就是说,如果我的视频是 10 秒,那么我会得到 300 帧(30 fps),但第一秒的帧每个重复 10 次。当请求 1 秒时,它类似于以 0.1 秒的时间返回帧。</p>

<blockquote>
<p>P.S: Though synchronous method is working fine, it is taking twice the
time taken by the asynchronous method. May be because it is returning
same frames. But I need it working to check the actual time usages.</p>
</blockquote></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您需要将 <code>requestedTimeToleranceBefore</code> 和 <code>requestedTimeToleranceAfter</code> 设置为您想要的精度,例如 <code>.zero</code> 但这可能会导致额外的延迟和成本生成图像。</p>

<p>例如:</p>

<pre><code>imageGenerator.requestedTimeToleranceBefore = .zero
imageGenerator.requestedTimeToleranceAfter = .zero
</code></pre>

<p>这样你就可以得到当时的准确帧。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - AVAssetImageGenerator.generateCGImagesAsynchronously 的帧不正确,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/50483128/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/50483128/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - AVAssetImageGenerator.generateCGImagesAsynchronously 的帧不正确