为什么我们需要在appendPixelBuffer之后暂停线程:withPresentationTime:?
问题描述:
当编码在iOS上的视频,大部分的解决方案包括此步骤:为什么我们需要在appendPixelBuffer之后暂停线程:withPresentationTime:?
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。如果我暂停线程,结果看起来很完美。
但我不明白“readyForMoreMediaData”的目的,如果我们需要暂停线程吗?看起来我可以将睡眠时间缩短到0.03,而不会导致看起来不稳定,但它仍然会减慢编码过程。
任何帮助将不胜感激,谢谢!
答
我已经使用assetwriter在多个应用中编写实时视频多年,其中包括以240 fps作为标准运行的视频。我没有生涩的视频问题。我从来没有使用任何睡眠命令,也没有使用CVBufferRelease。我的代码基本上是这样的:
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
if (videoWriterInput.readyForMoreMediaData) [videoWriterInput appendSampleBuffer:sampleBuffer])
}
也许你应该检查你如何设置你的assetwriter?有一个“recommendedVideoSettingsForAssetWriterWithOutputFileType”设置可以帮助您优化它。如果你不是绝对需要它的话,我会尝试不使用适配器,根据我的经验,它不需要更顺畅。
对不起,我应该说我使用OpenGL绘制对象,并将结果编码为视频。我不使用相机或现有视频作为源。我需要一个适配器,因为我需要一个pixelBufferPool。我会尝试“recommendedVideoSettingsForAssetWriterWithOutputFileType”。谢谢 – Xys
我认为recommendedVideoSettingsForAssetWriterWithOutputFileType只是用于“AVCaptureVideoDataOutput”,但我不使用这个 – Xys
如果你想看更多的代码,这里有一篇文章解释它:https://stackoverflow.com/a/9704392/5120292 – Xys