我想在视频中添加叠加图像。我使用 AVVideoComposition 和 CIFilter 来执行此操作,但未调用 AVAsynchronousCIImageFilteringRequest 处理程序。我放了一些断点(行 debugPrint“Here”,request.finish),但 XCode 没有达到断点。
我有视频,但没有水印。
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")
}
}
}