Hello, Try as for Applying filter on video File
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let savedoutputURL = documentsDirectory.appendingPathComponent("Filterd.mov")
try? FileManager.default.removeItem(at: savedoutputURL)
let urlVideofile = Bundle.main.url(forResource: "(your video file name)", withExtension: "mov")
let videoAsset = AVAsset(url: urlVideofile!)
let filter = CIFilter(name: "CIColorInvert")!
let composition = AVVideoComposition(asset: asset, applyingCIFiltersWithHandler: { request in
let source = request.sourceImage.clampingToExtent()
filter.setValue(source, forKey: kCIInputImageKey)
// Crop the invert output to the bounds of the original image
let output = filter.outputImage!.cropping(to: request.sourceImage.extent)
// Provide the filter output to the composition
request.finish(with: output, context: nil)
})
// Simultaneous Video playing although you can use as your's
let playerItem = AVPlayerItem(asset: videoAsset)
playerItem.videoComposition = composition
self.player = AVPlayer(playerItem: playerItem)
self.player.volume = 0.0
self.playerController.player = self.player
present(self.playerController, animated: true, completion: {
self.player.play()
})
Saving Video
let export = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPresetHighestQuality)
export!.outputFileType = AVFileType.mov
export!.outputURL = savedoutputURL
export!.videoComposition = composition
export?.exportAsynchronously(completionHandler: {
if export!.status.rawValue == 4 {
print("Export failed -> Reason: (export?.error!.localizedDescription))")
print(export?.error!)
return
}
print("done: ",savedoutputURL)
})
It is 100% tested code apply filter on 26 second video approx in 10 to 15 second
:)enjoy
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…