为什么我们需要在appendPixelBuffer:withPresentationTime之后暂停线程:?

时间:2017-09-01 14:30:35

标签: ios avfoundation video-encoding avassetwriter

在iOS上对视频进行编码时,大多数解决方案都包含以下步骤:

while(encoding) {
    if(assetWriterInput.readyForMoreMediaData) {
        [adaptor appendPixelBuffer:buffer withPresentationTime:presentTime];
        if(buffer)
            CVBufferRelease(buffer);
        [NSThread sleepForTimeInterval:0.05]; // <=== This line slows down encoding
    }
} 

如果我不在线程休眠,即使readyForMoreMediaData始终返回YES,结果视频也会显得不稳定。如果我暂停线程,结果看起来很完美。

但我不明白&#34; readyForMoreMediaData&#34;如果我们还需要暂停线程?看起来我可以将睡眠时间减少到0.03而不会让结果看起来生涩,但它仍然会减慢编码过程的速度。

任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

我已经使用资产编写者在几个应用程序中写了几年的实时视频,包括以240 fps作为标准运行的视频。我对生涩的视频没有问题。我从来没有使用任何睡眠命令和CVBufferRelease。我的代码基本上是这样的:

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer  fromConnection:(AVCaptureConnection *)connection
{
  if (videoWriterInput.readyForMoreMediaData) [videoWriterInput appendSampleBuffer:sampleBuffer]) 

}

也许你应该检查一下你如何设置资产编写者?有一个“recommendedVideoSettingsForAssetWriterWithOutputFileType”设置可以帮助您优化它。如果你不是绝对需要它,我会尝试没有适配器,根据我的经验,它没有运行更顺畅。