func applyfilters(){
do {
// movie input
movieInput = try MovieInput(url: inputVideoUrl, playAtActualSpeed: true, loop: false)
// movie output
movieOutput = try MovieOutput(URL: outputVideoUrl, size: videoSize, liveVideo: false)
// pipeline
movieInput.addTarget(currentFilter)
currentFilter.addTarget(renderView)
currentFilter.addTarget(movieOutput!)
movieOutput!.startRecording()
movieInput.start()
} catch {
print(error.localizedDescription)
}
}
//Below function should be called after the filter is applied on full video. If you call this function before video ends it will not generate the rest part of the video.
func stopVideoRecording(completion: (() -> Void)?) {
movieOutput?.finishRecording {
completion?()
}
}
//To save video
func saveVideo(){
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: outputVideoUrl)
}) { saved, error in
if saved {
let alertController = UIAlertController(title: "Your video was successfully saved", message: nil, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
Exemple d'utilisation
applyFilters()
DispatchQueue.main.asyncAfter(deadline:.now()+AVAsset(url:inputVideoUrl).duration.seconds, execute: {
stopVideoRecording{
self.saveVideo()
}
})