I'm working on an app that exports CALayer animations over 2-10 second videos using AVMutableVideoComposition
and AVVideoCompositionCoreAnimationTool
(export via AVExportSession
).
There can hundreds CAShapeLayers in each composition, and each will have animation(s) attached to it.
let animationLayer = CALayer()
animationLayer.frame = CGRectMake(0, 0, size.width, size.height)
animationLayer.geometryFlipped = true
// Add a ton of CAShapeLayers with CABasicAnimation's to animation Layer
let parentLayer = CALayer()
let videoLayer = CALayer()
parentLayer.frame = CGRectMake(0, 0, size.width, size.height)
videoLayer.frame = CGRectMake(0, 0, size.width, size.height)
parentLayer.addSublayer(videoLayer)
parentLayer.addSublayer(animationLayer)
mainCompositionInst.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, inLayer: parentLayer)
let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)
exportSession.outputURL = finalUrl
exportSession.outputFileType = AVFileTypeQuickTimeMovie
exportSession.shouldOptimizeForNetworkUse = true
exportSession.videoComposition = mainCompositionInst
exportSession.exportAsynchronouslyWithCompletionHandler(...)
Now, this totally works. However, the composition export can be very slow when the animations are numerous (15-25 secs to export). I'm interested in any ideas to speed up the export performance.
One idea I have thus far is to do multiple composition/export passes and add a "reasonable" number of animation layers each pass. But I have a feeling that would just make it slower.
Or, perhaps export lots of smaller videos that each contain a "reasonable" number of animation layers, and then compose them all together in a final export.
Any other ideas? Is the slowness just a fact of life? I'd appreciate any insight! I'm pretty novice with AVFoundation.