2015-10-19 24 views
0

我使用下面的代碼來追加PixelBuffer,但輸出視頻是黑屏(CIImage是正常的)。我認爲問題發生在newPixelBuffer中。AVAssetWriter寫入一個空白屏幕視頻

func recordRealTimeFilterVideoPerFrame(sampleBuffer: CMSampleBuffer, outputImage: CIImage) { 

     let formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer) 
     currentSampleTime = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer) 
     currentDimensions = CMVideoFormatDescriptionGetDimensions(formatDescription!) 

     guard assetWriter?.status == .Writing else { return } 
     guard (isRecording && assetWriterPixelBufferInput?.assetWriterInput.readyForMoreMediaData != nil) else { return } 
     guard let bufferPool = assetWriterPixelBufferInput?.pixelBufferPool else { print("bufferPool is nil"); return } 

     var newPixelBuffer = UnsafeMutablePointer<CVPixelBuffer?>.alloc(1) 
     CVPixelBufferPoolCreatePixelBuffer(nil, bufferPool, newPixelBuffer) 

     filterPreviewView?.ciContext.render(outputImage, 
              toCVPixelBuffer: newPixelBuffer.memory!, 
              bounds: outputImage.extent, 
              colorSpace: nil) 

     let success = assetWriterPixelBufferInput?.appendPixelBuffer(newPixelBuffer.memory!, withPresentationTime: currentSampleTime!) 

     if success == false { 
      print("pixel append false") 
     } 

     newPixelBuffer.destroy() 
     newPixelBuffer.dealloc(1) 
     newPixelBuffer = nil 
    } 
+0

首先你的第二個警戒聲明不檢查我認爲你打算檢查的內容。 「readyForMoreMediaData」是一個布爾值,你不應該比較零。如果編譯器不喜歡bool,那麼我認爲您的assetWriterInput必須是可選的,並且nil比較檢查的是assetWriterInput的值不是Optional.None。其次,我會打開你的邏輯,以便你可以添加打印語句或添加斷點,並查看出錯的地方。在這種情況下,打印語句可能會更好,因爲AVFoundation行爲可能會發生變化。 – SheffieldKevin

+0

感謝您的建議。「assetWriterPixelBufferInput?.assetWriterInput.readyForMoreMediaData!= nil」實際上什麼都不做,我的意思是「readyForMoreMediaData == true」。我找到了原因,當我合併視頻時,旋轉了視頻的屏幕。 – chun

回答

0

當我合併視頻時,我旋轉了它,使視頻離開屏幕,並導致黑屏。

0

您的currentSampleTime是否從零開始?

如果沒有,你應該從currentSampleTime所有後續值減去

currentSampleTime = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer) 

的第一個值。

+1

感謝您的建議,但我的assetWriter像in「assetWriter?.startSessionAtSourceTime(currentSampleTime)」,我找到了原因,當我合併視頻時,我旋轉了它,使視頻離開了屏幕。 – chun

+0

一切順利。你能回答你的問題並接受嗎? –

+0

不,我的意思是你的回答,不是我的,這是錯誤的! –