I am using the following code to append PixelBuffer, but the output video is an black screen(CIImage is normal). I think the problem occurred in newPixelBuffer.
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
}