AVAssetWriter 写一段黑屏视频

AVAssetWriter writes a blank screen video

我正在使用以下代码附加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
    }

您的 currentSampleTime 是从零开始的吗?

如果不是,您应该减去

的第一个值
 currentSampleTime  = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer)

来自 currentSampleTime 的所有后续值。

当我合并视频时,我旋转它使视频超出屏幕,导致黑屏。