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

时间:2018-02-15 22:29:37

标签: ios swift avfoundation 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.")
                }
            }
        }
    }
}

我收到 newPixelBuffer 为零的错误,但同样,只有7.9" iPad兼容。 iPad Pro功能无任何错误。有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

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

java.lang.NoClassDefFoundError: org/springframework/beans/factory/config/BeanDefinitionCustomizer at org.springframework.context.annotation.AnnotatedBeanDefinitionReader.registerBean(AnnotatedBeanDefinitionReader.java:145) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.context.annotation.AnnotatedBeanDefinitionReader.register(AnnotatedBeanDefinitionReader.java:135) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:158) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE] at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:135) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE] at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:127) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE] at org.springframework.boot.SpringApplication.load(SpringApplication.java:701) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE] at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:390) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:325) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE] at com.example.demo.DemoApplication.main(DemoApplication.java:9) [classes/:na] Caused by: java.lang.ClassNotFoundException: org.springframework.beans.factory.config.BeanDefinitionCustomizer at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.8.0_161] at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_161] at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) ~[na:1.8.0_161] at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_161] ... 11 common frames omitted

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

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

let codec: AVVideoCodecType = AVVideoCodecType.hevc,问题现已消失。