【问题标题】:Difficulty Writing Pixel Buffer To Asset Writer on Certain Devices在某些设备上难以将像素缓冲区写入资产写入器
【发布时间】:2018-07-26 19:21:53
【问题描述】:

我正在我的应用程序中开发一个函数,将图像从我的示例缓冲区写入 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 为 nil,但同样,仅在 7.9 英寸 iPad 上。iPad Pro 正常运行,没有任何错误。有什么想法吗?谢谢!

【问题讨论】:

  • CVPixelBufferPoolCreatePixelBuffer的调用返回值是多少?
  • 在有问题的设备上运行时,我收到的只是Thread 3: Unexpectedly found nil while unwrapping an Optional value
  • 将不得不问 Valérian 提出的相同问题。 CFPixelBufferPoolCreatePixelBuffer 返回什么?根据文档,该函数返回一个CVReturn,其中包含一个错误值,这可能解释了它失败的原因。这是什么?
  • 不确定我这样做是否正确,但我设置了var status:CVReturn = CVPixelBufferPoolCreatePixelBuffer(nil, (self.videoWriter?.assetWriterPixelBufferInput!.pixelBufferPool!)!, &newPixelBuffer)。崩溃时,返回65552448。如果这可能不正确,请告诉我。

标签: ios swift avfoundation cvpixelbuffer


【解决方案1】:

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

let codec: AVVideoCodecType = AVVideoCodecType.hevc

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

我已将我的编解码器选择更改为:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    相关资源
    最近更新 更多