如何将MTLTexture转换为CVpixelBuffer以写入AVAssetWriter? [英] How to convert a MTLTexture to CVpixelBuffer to write into an AVAssetWriter?

查看:790
本文介绍了如何将MTLTexture转换为CVpixelBuffer以写入AVAssetWriter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在实况视频上应用过滤器,而我尝试在Metal中进行过滤.

I have a requirement to apply filters on the live video and I'm trying to do it in Metal.

但是在将过滤器编码为目标过滤器后,我遇到了将MTLTexture转换为CVPixelBuffer的问题.参考( https://github.com/oklyc/MetalCameraSample-master-2 )

But I have encountered problem with converting the MTLTexture into CVPixelBuffer after encoding the filter into destination filter. Reference (https://github.com/oklyc/MetalCameraSample-master-2)

这是我的密码.

if let pixelBuffer = pixelBuffer {
                CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags.init(rawValue: 0))             
                let region = MTLRegionMake2D(0, 0, Int(currentDrawable.layer.drawableSize.width), Int(currentDrawable.layer.drawableSize.height))                    
                let bytesPerPixel = 4;
                let bytesPerRow = CGFloat(bytesPerPixel) * currentDrawable.layer.drawableSize.width

                let tempBuffer = CVPixelBufferGetBaseAddress(pixelBuffer)
                destinationTexture.getBytes(tempBuffer!, bytesPerRow: Int(bytesPerRow), from: region1, mipmapLevel: 0)

                let image = self.imageFromCVPixelBuffer(buffer: pixelBuffer)
                CVPixelBufferUnlockBaseAddress(pixelBuffer, CVPixelBufferLockFlags.init(rawValue: 0))

            }

imageFromCVPixelBuffer方法看起来像这样.

The method imageFromCVPixelBuffer looks like this.

func imageFromCVPixelBuffer(buffer: CVPixelBuffer) -> UIImage {

    let ciimage = CIImage(cvPixelBuffer: buffer)
    let context = CIContext(options: nil)
    let cgimgage = context.createCGImage(ciimage, from: CGRect(x: 0, y: 0, width: CVPixelBufferGetWidth(buffer), height: CVPixelBufferGetHeight(buffer)))

    let uiimage = UIImage(cgImage: cgimgage!)

    return uiimage
}

这是通过金属渲染图像的屏幕截图

Here is the screen shot of the image rendering through metal

这是将MTLTexture转换为CVPixelBuffer的同一图像的屏幕截图.

Here is the screen shot of the same image converting MTLTexture to CVPixelBuffer.

需要将MTLtexture转换为CVPixelBuffer才能写入AVAssetWriter,然后将其保存到库中.

Converting MTLtexture into CVPixelBuffer is required to write into an AVAssetWriter and then saving it to the Library.

推荐答案

不要自己计算bytesPerRow.它被传递给Metal,以让Metal知道如何排列行.您希望Metal按照CVPixelBuffer期望的方式安排它们.因此,您应该使用CVPixelBufferGetBytesPerRow()确定该值.

Don't compute bytesPerRow yourself like that. It's being passed in to Metal to let Metal know how to arrange the rows. You want Metal to arrange them the way CVPixelBuffer expects them. Therefore, you should use CVPixelBufferGetBytesPerRow() to determine the value.

这篇关于如何将MTLTexture转换为CVpixelBuffer以写入AVAssetWriter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆