Setting the AVMutableVideoComposition statement causes the handler to be called

I have a function to filter an AVPlayerItem resource. One of the problems was setting up the video conversion. However, when I set AVMutableVideoCompositionInstruction

for AVMutableVideoComposition

, the handler is no longer called.

Here is my code:

private func filter(playerItem: AVPlayerItem) {

    let videoComposition = AVMutableVideoComposition(asset: playerItem.asset, applyingCIFiltersWithHandler: { (request) in
        print("Composing") // does not print whenever the instructions are added
        if let filteredImage = filterImage(request.sourceImage) {
            request.finish(with: filteredImage, context: nil)
        } else {
            request.finish(with: RenderError.couldNotFilter) // An error
        }
    })

    guard let videoTrack = playerItem.asset.tracks(withMediaType: .video).first else { return }

    let size = CGSize(width: videoTrack.naturalSize.height, height: videoTrack.naturalSize.width)
    videoComposition.renderSize = size

    let videoInstruction = AVMutableVideoCompositionInstruction()
    videoInstruction.timeRange = CMTimeRange(start: kCMTimeZero, duration: playerItem.asset.duration)

    let transformInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack)
    let translate = CGAffineTransform(translationX: size.width, y: size.height)
    let rotate = CGAffineTransform(rotationAngle: CGFloat.pi)
    transformInstruction.setTransform(translate.concatenating(rotate), at: kCMTimeZero)
    videoInstruction.layerInstructions.append(transformInstruction)
    videoComposition.instructions.append(videoInstruction)

    playerItem.videoComposition = videoComposition
}

      

Why handler

isn't it called anymore and how to fix it?

I'll give you that many cake points if you can answer!

+3


source to share


1 answer


I have filed a bug with Apple and it appears this behavior is not a bug. This was their answer:

Engineering provided the following information on this issue:

CoreImage correction and layered instruction composition cannot be used at the same time. Layer instructions will fail when added to an AVMutableVideoComposition that is initialized with + [videoCompositionWithAsset: applyCIFiltersWithHandler:]. To use layer instructions in this case, move the functionality to a handler instead of adding layer instructions to AVMutableVideoComposition.



This explains why the instructions don't seem to do anything and the handler isn't called. Instead of using instructions, it is said to move the transformation functionality into a handler; unfortunately I don't quite understand how to implement this solution - this is another question.

+2


source







All Articles