Video rotated after applying AVVideoComposition

后端 未结 5 1282
野性不改
野性不改 2021-02-09 07:00

After applying an AVVideoComposition to my AVPlayerItem, the filter I apply does work, but the video gets rotated in the AVPlayerLayer.

5条回答
  •  长情又很酷
    2021-02-09 07:18

    What worked for me at the end:

    private func filterVideo(with filter: Filter?) {
        guard let player = playerLayer?.player, let playerItem = player.currentItem else { return }
    
        let videoComposition = AVVideoComposition(asset: playerItem.asset, applyingCIFiltersWithHandler: { (request) in
            if let filter = filter {
                if let filteredImage = filter.filterImage(request.sourceImage) {
                    let output = filteredImage.cropping(to: request.sourceImage.extent)
                    request.finish(with: output, context: nil)
                } else {
                    printError("Image not filtered")
                    request.finish(with: RenderError.couldNotFilter)
                }
            } else {
                let output = request.sourceImage.cropping(to: request.sourceImage.extent)
                request.finish(with: output, context: nil)
            }
        })
    
        playerItem.videoComposition = videoComposition
    }
    

    This is the filterImage function of Filter, which is just a nice little wrapper for CIFilter:

    func filterImage(_ ciImage: CIImage) -> CIImage? {
        guard let filter = ciFilter else { return nil }
        filter.setDefaults()
        filter.setValue(ciImage, forKey: kCIInputImageKey)
        guard let filteredImageData = filter.value(forKey: kCIOutputImageKey) as? CIImage else { return nil }
        return filteredImageData
    }
    

提交回复
热议问题