AVAssetWriter写入空白屏幕视频

时间:2015-10-19 10:34:00

标签: ios swift avfoundation avassetwriter

我使用以下代码附加PixelBuffer,但输出视频是黑屏(CIImage是正常的)。我认为问题发生在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
    }

2 个答案:

答案 0 :(得分:0)

你的currentSampleTime从零开始吗?

如果没有,你应该减去

的第一个值
 currentSampleTime  = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer)

来自currentSampleTime的所有后续值。

答案 1 :(得分:0)

当我合并视频时,我将其旋转,使视频脱离屏幕,并导致黑屏。