Even when I don't apply any filters the performance of AVAssetExportSession exportAsynchronously is very very bad. It takes more than 10 seconds or so to export a 26 second video. How can I fix that?
let avPlayerItem = AVPlayerItem(asset: video)
avVideoComposition = AVVideoComposition(asset: avPlayerItem.asset, applyingCIFiltersWithHandler: { request in
request.finish(with: request.sourceImage, context: nil)
})
avPlayerItem.videoComposition = avVideoComposition
// -----------
func exportFilterVideo(videoComposition:AVVideoComposition , completion: @escaping
(_ outputURL : NSURL?) -> ()) {
let exportSession = AVAssetExportSession(asset: self, presetName: AVAssetExportPresetHighestQuality)!
let croppedOutputFileUrl = URL( fileURLWithPath: NSTemporaryDirectory() + NSUUID().uuidString + ".mov")
exportSession.outputURL = croppedOutputFileUrl
exportSession.outputFileType = AVFileType.mov
exportSession.videoComposition = videoComposition
exportSession.exportAsynchronously(completionHandler: {
guard exportSession.status != .failed else {
completion(nil)
return
}
if exportSession.status == .completed {
DispatchQueue.main.async(execute: {
completion(croppedOutputFileUrl as NSURL)
})
}
else {
completion(nil)
}
})
}
even when I use AVAssetExportPresetLowQuality it does not help!
First solution:
if you've tried exportSession with AVAssetExportPresetMediumQuality & AVAssetExportPresetLowQuality and not helped! then consider AVAssetExportPresetHighestQuality
A preset to export a high-quality movie file.
Then try setting the videoComposition to nil if you do not need to apply any filters , cause as it says in the videoComposition
Try with .mp4 video encoding formats as it may perform better than .mov
And if you need your video to be optimized for network usage then try this
But please note that this may take a bit longer with the export session!
Second solution:
I've experienced the same problem a while ago and I ended up solving this issue by first compressing the video and then apply changes to it which improve the whole speed of the program.
I used the following library
LightCompressor_iOS
LightCompressor_example
and this is a sample code
video editor iOS app (just for reference)
You may fetch and run the app from the following project as it is an iOS app for applying filters, stickers and images to videos so that you can recognize how it perform on your device. OptiVideoEditor-for-iOS
I think the code that you would be interested to read is in the following link
OptiVideoEditor