I want to add an overlay image into a video. I use AVVideoComposition and CIFilter to do this, but AVAsynchronousCIImageFilteringRequest handler not called. I put some breakpoints (line debugPrint"Here", request.finish), but XCode doesn't hit the breakpoints.
I got the video, but doesn't have the watermark.
func watermark(video asset: AVAsset, with image: UIImage, output outURL: URL) {
guard let watermarkImage = CIImage(image: image) else {
return
}
let context = CIContext(options: nil)
let videoComposition = AVVideoComposition(asset: asset) { (request) in
debugPrint("Here")
let source = request.sourceImage.clampedToExtent()
let watermarkFilter = CIFilter(name: "CISourceOverCompositing")
watermarkFilter?.setValue(source, forKey: kCIInputBackgroundImageKey)
let transform = CGAffineTransform(translationX: request.sourceImage.extent.width - watermarkImage.extent.width - 10, y: 10)
watermarkFilter?.setValue(watermarkImage.transformed(by: transform), forKey: kCIInputImageKey)
guard let outputImage = watermarkFilter?.outputImage else {
return
}
request.finish(with: outputImage, context: context)
}
let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetPassthrough)
exporter?.outputFileType = .mov
exporter?.outputURL = outURL
exporter?.videoComposition = videoComposition
exporter?.exportAsynchronously { [weak exporter] in
guard let export = exporter else {
return
}
switch export.status {
case .failed:
print("failed \(exporter?.error)")
break
case .cancelled:
print("cancelled \(exporter?.error)")
break
case .completed:
print("complete")
default:
print("default")
}
}
}
You shouldn't use passthrough preset with CI filtering. Try using another preset that defines a video format instead.
This docs doesn't mention CI filtering specifically but it states that using passthrough preset would lead to layer instructions being ignored. I would expect it is the same for CI filtering.
With some help, I figured out, I used preset AVAssetExportPresetPassthrough, AVAssetExportSession ignore the filter. I changed the preset, now it works.