难以在某些设备上将像素缓冲区写入资产编写器

Zba*_*itZ 9 avfoundation ios swift cvpixelbuffer

我正在我的应用程序中的一个函数将我的示例缓冲区中的图像写入AVAssetWriter.奇怪的是,这在10.5英寸iPad Pro上运行良好,但在7.9英寸iPad Mini 2上造成了崩溃.我无法理解相同的代码在两个不同的设备上是如何出问题的.但这是我的代码;

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {

    // Setup the pixel buffer image
    let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!

    // Setup the format description
    let formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer)!

    // Setup the current video dimensions
    self.currentVideoDimensions = CMVideoFormatDescriptionGetDimensions(formatDescription)

    // Setup the current sample time
    self.currentSampleTime = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer)

    // Handle record
    if self.isCapturing {

        // Setup auto release pool
        autoreleasepool {

            // Setup the output image
            let outputImage = CIImage(cvPixelBuffer: pixelBuffer)

            // Ensure the video writer is ready for more data
            if self.videoWriter?.assetWriterPixelBufferInput?.assetWriterInput.isReadyForMoreMediaData == true {

                // Setup the new pixel buffer (THIS IS WHERE THE ERROR OCCURS)
                var newPixelBuffer: CVPixelBuffer? = nil

                // Setup the pixel buffer pool
                CVPixelBufferPoolCreatePixelBuffer(nil, (self.videoWriter?.assetWriterPixelBufferInput!.pixelBufferPool!)!, &newPixelBuffer)

                // Render the image to context
                self.context.render(outputImage, to: newPixelBuffer!, bounds: outputImage.extent, colorSpace: nil)

                // Setup a success case
                let success = self.videoWriter?.assetWriterPixelBufferInput?.append(newPixelBuffer!, withPresentationTime: self.currentSampleTime!)

                // Ensure the success case exists
                guard let mySuccess = success else { return }

                // If unsuccessful, log
                if !mySuccess {
                    print("Error with the sample buffer.  Check for dropped frames.")
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到一个错误,newPixelBuffer是零,但再次,只有7.9" iPad.iPad Pro功能没有任何错误.任何想法?谢谢!

Zba*_*itZ 1

我最终通过将问题追溯到我在资产编写器的视频输出设置中选择的编解码器来解决这个问题。我的编解码器设置为:

let codec: AVVideoCodecType = AVVideoCodecType.hevc

在做一些研究时,我发现了这篇文章,它表明只有某些设备可以捕获HEVC 中的媒体。由于我的第一台设备是 10.5 英寸 iPad Pro,因此它可以毫无问题地捕获媒体。我的第二台设备是 iPad Mini,这导致每次我尝试捕获时都会出现最初的问题。

此后我将编解码器选择更改为:

let codec: AVVideoCodecType = AVVideoCodecType.h264,这个问题现在已经消失了。