2017-09-01 62 views
0

當編碼在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,而不會導致看起來不穩定,但它仍然會減慢編碼過程。

任何幫助將不勝感激,謝謝!

回答

0

我已經使用assetwriter在多個應用中編寫實時視頻多年,其中包括以240 fps作爲標準運行的視頻。我沒有生澀的視頻問題。我從來沒有使用任何睡眠命令,也沒有使用CVBufferRelease。我的代碼基本上是這樣的:

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

} 

也許你應該檢查你如何設置你的assetwriter?有一個「recommendedVideoSettingsForAssetWriterWithOutputFileType」設置可以幫助您優化它。如果你不是絕對需要它的話,我會嘗試不使用適配器,根據我的經驗,它不需要更順暢。

+0

對不起,我應該說我使用OpenGL繪製對象,並將結果編碼爲視頻。我不使用相機或現有視頻作爲源。我需要一個適配器,因爲我需要一個pixelBufferPool。我會嘗試「recommendedVideoSettingsForAssetWriterWithOutputFileType」。謝謝 – Xys

+0

我認爲recommendedVideoSettingsForAssetWriterWithOutputFileType只是用於「AVCaptureVideoDataOutput」,但我不使用這個 – Xys

+0

如果你想看更多的代碼,這裏有一篇文章解釋它:https://stackoverflow.com/a/9704392/5120292 – Xys

相關問題