我使用下面的代碼來追加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
}
首先你的第二個警戒聲明不檢查我認爲你打算檢查的內容。 「readyForMoreMediaData」是一個布爾值,你不應該比較零。如果編譯器不喜歡bool,那麼我認爲您的assetWriterInput必須是可選的,並且nil比較檢查的是assetWriterInput的值不是Optional.None。其次,我會打開你的邏輯,以便你可以添加打印語句或添加斷點,並查看出錯的地方。在這種情況下,打印語句可能會更好,因爲AVFoundation行爲可能會發生變化。 – SheffieldKevin
感謝您的建議。「assetWriterPixelBufferInput?.assetWriterInput.readyForMoreMediaData!= nil」實際上什麼都不做,我的意思是「readyForMoreMediaData == true」。我找到了原因,當我合併視頻時,旋轉了視頻的屏幕。 – chun