So I have two videos assets - one for background and one for foreground. What I want to achieve is to apply a CIFilter (ChromaKey) to the foreground video then add the background video so the two overlap. I can do the overlap or applying the filter but I can't put it together. Here is my code for videos overlap.
Thanks for any help!
Code Block let bgAsset = AVURLAsset(url: URL(fileURLWithPath: Bundle.main.path(forResource: "bg", ofType: "mp4")!)) let fgAsset = AVURLAsset(url: URL(fileURLWithPath: Bundle.main.path(forResource: "fg", ofType: "mp4")!)) let mixComposition = AVMutableComposition() let bgTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)! let fgTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)! try! bgTrack.insertTimeRange(CMTimeRange(start: .zero, duration: bgAsset.duration), of: bgAsset.tracks(withMediaType: .video).first!, at: .zero) try! fgTrack.insertTimeRange(CMTimeRange(start: .zero, duration: bgAsset.duration), of: fgAsset.tracks(withMediaType: .video).first!, at: .zero) let mainInstruction = AVMutableVideoCompositionInstruction() mainInstruction.timeRange = CMTimeRange(start: .zero, duration: bgAsset.duration) let firstLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: bgTrack) let secondLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: fgTrack) mainInstruction.layerInstructions = [secondLayerInstruction, firstLayerInstruction] let videoComposition = AVMutableVideoComposition() videoComposition.instructions = [mainInstruction] videoComposition.frameDuration = CMTime(value: 1, timescale: 30) videoComposition.renderSize = CGSize(width: 640, height: 480) let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("overlapVideo.mov") let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)! exporter.outputURL = url exporter.videoComposition = videoComposition exporter.outputFileType = .mov exporter.exportAsynchronously { print("finished") }
Thanks for any help!