Open taontech opened 2 years ago
// https://user-images.githubusercontent.com/990488/164454078-0c6e6366-c34c-4da0-ade5-5113de4b8569.mov
func buffer(from ciimage:CIImage) -> CVPixelBuffer?{ let attrs = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue, kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue] as CFDictionary var pixelBuffer : CVPixelBuffer? let status = CVPixelBufferCreate(kCFAllocatorDefault, Int(ciimage.extent.width), Int(ciimage.extent.height), kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange, attrs, &pixelBuffer) guard (status == kCVReturnSuccess) else { return nil } let context = CIContext() let cs = context.workingColorSpace print(cs) context.render(ciimage, to: pixelBuffer!, bounds: ciimage.extent, colorSpace: CGColorSpace(name: CGColorSpace.itur_2100_HLG )) return pixelBuffer }
let outputSize = (self.imageView?.image?.size)! let outputSettings = [ AVVideoCodecKey: AVVideoCodecType.hevc, AVVideoWidthKey : NSNumber(value: Float(outputSize.width)), AVVideoHeightKey : NSNumber(value: Float(outputSize.height)), AVVideoColorPropertiesKey : [AVVideoTransferFunctionKey: AVVideoTransferFunction_ITU_R_2100_HLG, AVVideoColorPrimariesKey: AVVideoColorPrimaries_ITU_R_2020, AVVideoYCbCrMatrixKey: AVVideoYCbCrMatrix_ITU_R_2020 ], AVVideoCompressionPropertiesKey: [AVVideoProfileLevelKey:kVTProfileLevel_HEVC_Main10_AutoLevel/*,AVVideoAverageBitRateKey:NSNumber(value: Float(3000000))*/ ,AVVideoQualityKey:NSNumber(value: Float(0.85)),AVVideoMaxKeyFrameIntervalKey:NSNumber(value: Float(250)),AVVideoMaxKeyFrameIntervalDurationKey:NSNumber(value: Float(10))] ] as [String:Any] guard videoWriter.canApply(outputSettings: outputSettings, forMediaType: .video) else { fatalError("输出设定不支持") }
先看效果
// https://user-images.githubusercontent.com/990488/164454078-0c6e6366-c34c-4da0-ade5-5113de4b8569.mov
转换过程中几个关键节点
CVPixelBuffer的创建
OutputSetting的设置