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")
}
}
}