Search code examples
iosswiftavfoundationcifilter

iOS - Merge multiple videos applying CIFilter


I want to merge two or more videos (stored on disk) in one file. It has worked great with AVAssetExportSession. But next problem is to apply different CIFilter for each video. For applying CIFilter for one video I've used AVMutableVideoComposition(asset:applyingCIFiltersWithHandler:) Is it possible to merge two (and mode) AVMutableVideoCompositions without saving each video with filter on disk separately? Or should I use AVAssetReader/Writer for this purpose?


Solution

  • Yes, it's possible. However, it's a bit more complicated, unfortunately. And you need a lot more glue code.

    You can fully customize how multiple videos are composed (and what happens to the single frames before) by implementing a custom compositor that implements the AVVideoCompositing protocol. There is an old example project from Apple showcasing the whole process. There's also this question and answer on StackOverflow that you can refer to.

    The basic idea is that you implement your filter and composition logic in the startRequest(_:) method of your custom compositor. There you'll have access to the frames of all input videos. So you can process and compose them to your liking.