10

我正在我的应用程序中开发一个函数,将图像从我的示例缓冲区写入 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.")
                }
            }
        }
    }
}

我收到newPixelBuffer为零的错误,但同样,仅在 7.9 英寸 iPad 上。iPad Pro 正常运行,没有任何错误。有什么想法吗?谢谢!

4

1 回答 1

0

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

let codec: AVVideoCodecType = AVVideoCodecType.hevc

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

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

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

于 2018-05-17T16:14:45.573 回答