在某些设备上难以将像素缓冲区写入资产编写器
Difficulty Writing Pixel Buffer To Asset Writer on Certain Devices
我正在我的应用程序中开发一个功能,将图像从我的示例缓冲区写入 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 功能正常,没有任何错误。有什么想法吗?谢谢!
通过将问题追溯到我在 Asset Writer 的视频输出设置中选择的编解码器,我最终解决了这个问题。我将编解码器设置为:
let codec: AVVideoCodecType = AVVideoCodecType.hevc
在做一些研究时,我发现了 this 文章,其中指出只有某些设备可以 捕获 HEVC 中的媒体。由于我的第一台设备是 10.5" iPad Pro,它可以毫无问题地捕获媒体。我的第二台设备是 iPad Mini,这导致我每次尝试捕获时都会出现原来的问题。
我已经将编解码器选择更改为:
let codec: AVVideoCodecType = AVVideoCodecType.h264
,问题现已消失。
我正在我的应用程序中开发一个功能,将图像从我的示例缓冲区写入 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 功能正常,没有任何错误。有什么想法吗?谢谢!
通过将问题追溯到我在 Asset Writer 的视频输出设置中选择的编解码器,我最终解决了这个问题。我将编解码器设置为:
let codec: AVVideoCodecType = AVVideoCodecType.hevc
在做一些研究时,我发现了 this 文章,其中指出只有某些设备可以 捕获 HEVC 中的媒体。由于我的第一台设备是 10.5" iPad Pro,它可以毫无问题地捕获媒体。我的第二台设备是 iPad Mini,这导致我每次尝试捕获时都会出现原来的问题。
我已经将编解码器选择更改为:
let codec: AVVideoCodecType = AVVideoCodecType.h264
,问题现已消失。