0

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
    }
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
chun
  • 1
  • 1
  • 4
  • First up your second guard statement doesn't check what I think you are intending to check. "readyForMoreMediaData" is a bool and you shouldn't be comparing against nil. If the compiler didn't like a bool then I think your assetWriterInput must be an optional and what the nil compare is checking is that the value for assetWriterInput is not Optional.None. Secondly I would unwrap your logic a bit so that you can add print statements or add break points and see where things go wrong. Print statements might be better as with break points in this case AVFoundation behaviour might change. – SheffieldKevin Oct 20 '15 at 09:38
  • Thanks for your advice."assetWriterPixelBufferInput?.assetWriterInput.readyForMoreMediaData != nil" indeed do nothing,I meant for "readyForMoreMediaData == true".And I found the reason,when I merge videos,I rotated it which make the video out of the screen. – chun Oct 21 '15 at 02:39

2 Answers2

0

Does your currentSampleTime start at zero?

If not, you should subtract the very first value of

 currentSampleTime  = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer)

from all the subsequent values of currentSampleTime.

Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159
  • 1
    Thanks for your advice, but my assetWriter inited like "assetWriter?.startSessionAtSourceTime(currentSampleTime)", And I found the reason,when I merge videos,I rotated it which make the video out of the screen. – chun Oct 21 '15 at 02:49
  • All's well that ends well. Can you answer your question and accept it? – Rhythmic Fistman Oct 21 '15 at 02:51
0

When I merged videos, I rotated it which make the video out of the screen, and result in a black screen.

chun
  • 1
  • 1
  • 4