AVVideoComposition与CIFilters崩溃

时间:2018-10-12 14:28:38

标签: ios avfoundation avassetexportsession avvideocomposition

我正在通过CIFilters创建AVVideoComposition:

 videoComposition = AVMutableVideoComposition(asset: asset, applyingCIFiltersWithHandler: {[weak self] request in  

            // Clamp to avoid blurring transparent pixels at the image edges  
            let source = request.sourceImage.clampedToExtent()  
            let output:CIImage  

            if let filteredOutput = self?.runFilters(source, filters: filters)?.cropped(to: request.sourceImage.extent) {  
                output = filteredOutput  
            } else {  
                output = source  
            }  

            // Provide the filter output to the composition  
            request.finish(with: output, context: nil)  
        })  

然后为了正确处理旋转,我创建了一条传递指令,该指令在传递层上设置身份转换。

     let passThroughInstruction = AVMutableVideoCompositionInstruction()  
     passThroughInstruction.timeRange = CMTimeRangeMake(start: CMTime.zero, duration: asset.duration)  
     let passThroughLayer = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack)  
            passThroughLayer.setTransform(CGAffineTransform.identity, at: CMTime.zero)   
     passThroughInstruction.layerInstructions = [passThroughLayer]  
            videoComposition.instructions = [passThroughInstruction]  

问题是它崩溃并显示错误:

'*** -[AVCoreImageFilterCustomVideoCompositor startVideoCompositionRequest:] Expecting video composition to contain only AVCoreImageFilterVideoCompositionInstruction'  

我的问题是,如果我未指定此passThroughInstruction,则如果输入资产的视频轨包含指定90度旋转的preferredTransform,则输出不正确。如何将视频合成与可正确处理视频轨道的preferredTransform的Core Image滤镜一起使用?

编辑:该问题看起来相似,但与涉及播放的其他问题有所不同。就我而言,回放很好,但是渲染会产生失真的视频。

1 个答案:

答案 0 :(得分:0)

好的,我找到了真正的问题。问题是应用由AVMutableVideoComposition(asset :, ApplyingCIFiltersWithHandler :)函数返回的videoComposition隐式创建一个合成指令,该指令可物理旋转CIImage,以防在视频轨道上通过preferredTransform施加旋转变换的情况。是否应该执行此操作尚有争议,因为preferredTransform是在播放器级别应用的。作为解决方法,除了在this答案中建议的内容之外。我必须调整通过videoSettings中的AVVideoWidthKey,AVVideoHeightKey传递给AVAssetReader / Writer的宽度和高度。