3

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.

Kyle Truscott
  • 1,537
  • 1
  • 12
  • 18

1 Answers1

1

I went down the video composition path and didn't like the constant frame rate that AVAssetExportSession enforced, so I manually rendered my content onto AVAssetReader's output and encoded it to mov with AVAssetWriter.

If you have a lot of content, you could translate it to OpenGL/Metal and use the GPU to render it blazingly quickly directly onto your video frames via texture caches.

I bet the GPU wouldn't break a sweat, so you'd be limited by the video encoder speed. I'm not sure what that is - the 4s used to do 3x realtime, that can only have improved.

There's a lot of fiddling required to do what I suggest. You could get started very quickly on the OpenGL path by using GPUImage, although a Metal version would be very cool.

Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159
  • What is the drawback to constant frame rate? Was it too low for certain segments and compromised video quality, or ... ? – Crashalot Jan 22 '16 at 21:12
  • 1
    Basically, constant frame rate increased the size of the file and export times with no increase in quality. I was transforming a video file (it was an export feature for a vj video scratch app, so a continuous transformation of the video timeline), so 1. the input frame rate was already variable (due to lighting/exposure) and 2. for the slower than realtime case, many frames would be repeats at constant frame rate. p.s. letting the rate drop _too_ low can cause playback compatibility problems. – Rhythmic Fistman Jan 22 '16 at 22:56
  • Cool thanks! We merge user videos with UIImageView layers and AVVideoCompositionCoreAnimationTool is very slow. Do you have specific suggestions on improving AVVideoCompositionCoreAnimationTool performance? Another workaround is to record video of only a UIView and merge that video with the user videos using only AVMutableComposition, which should be much faster. Do you know if producing video of only a UIView is possible, question here: http://stackoverflow.com/questions/34956713/simplified-screen-capture-record-video-of-only-what-appears-within-the-layers-o – Crashalot Jan 22 '16 at 23:03
  • @Crashalot Do you found any solution for improving AVVideoCompositionCoreAnimationTool performance? – Carolina Dec 18 '16 at 00:01