I want to apply CIFilter on video and save that filter applied video.
I am using AVMutableVideoComposition (also tried with AVVideoComposition) for apply filter on video and its working fine with AVPlayer but when i am exporting filtered video then it takes too much time to save locally (20sec video taking arround 6-8 min). Please guide me what i am doing wrong... Thankyou !
let item = AVPlayerItem(asset: currentAsset!)
let videoComposition = AVMutableVideoComposition(asset: currentAsset!) { request in
let filter = CIFilter(name: "CIColorInvert")!
let source = request.sourceImage.clampedToExtent()
filter.setValue(source, forKey: kCIInputImageKey)
let output = filter.outputImage!.cropped(to: request.sourceImage.extent)
// Provide the filter output to the composition
request.finish(with: output, context: nil)
}
item.videoComposition = videoComposition
self.player.replaceCurrentItem(with: item)
let exporter = AVAssetExportSession(asset: item.asset, presetName: AVAssetExportPresetHighestQuality)
exporter?.videoComposition = videoComposition
exporter?.outputFileType = .mp4
let filename = "filename.mp4"
let documentsDirectory = FileManager.default.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask).last!
let outputURL = documentsDirectory.appendingPathComponent(filename)
exporter?.outputURL = outputURL
exporter?.exportAsynchronously(completionHandler: {
guard exporter?.status == .completed else {
print("export failed: \(exporter?.error)")
return
}
print("done: ",outputURL)
}
)