How to convert MTLTexture to CVpixelBuffer for recording in AVAssetWriter?

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

But I ran into the problem of converting MTLTexture to CVPixelBuffer after encoding the filter into the destination filter. Link ( https://github.com/oklyc/MetalCameraSample-master-2 )

Here are my codes.

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))

            }

The imageFromCVPixelBuffer method is as follows.

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 a screen shot of rendering an image through metal

enter image description here

Here is a screenshot of the same image that converts MTLTexture to CVPixelBuffer.

enter image description here

Converting MTLtexture to CVPixelBuffer is required to write to AVAssetWriter and then save it to the library.

+2
source share
1

bytesPerRow . Metal, Metal , . , , CVPixelBuffer. CVPixelBufferGetBytesPerRow().

+5

Source: https://habr.com/ru/post/1598887/


All Articles