I am developing an application that include functionality to play video with per-frame animation. You can see an example of such functionality.

I already tried to add CAKeyFrameAnimation to sublayer of AVSynchronizedLayer and have some troubles with it.

I also already tried to pre-render video with AVAssetExportSession, and it is working perfectly. But it's very slow. It needa up to 3 minutes to render such video.

Maybe there are other approaches to make it?


This is how I implement animation with AVSynchronizedLayer:

let fullScreenAnimationLayer = CALayer()
fullScreenAnimationLayer.frame = videoRect
fullScreenAnimationLayer.geometryFlipped = true

values: [NSValue] = [], times: [NSNumber] = []

// fill values array with positions of face center for each frame
values.append(NSValue(CATransform3D: t))

// fill times with corresoinding time for each frame
times.append(NSNumber(double: (Double(j) / fps) / videoDuration)) // where fps = 25 (according to video file fps)


let transform = CAKeyframeAnimation(keyPath: "transform")
transform.duration = videoDuration
transform.calculationMode = kCAAnimationDiscrete
transform.beginTime = AVCoreAnimationBeginTimeAtZero
transform.values = values
transform.keyTimes = times
transform.removedOnCompletion = false
transform.fillMode = kCAFillModeForwards
fullScreenAnimationLayer.addAnimation(transform, forKey: "a_transform")


if let syncLayer = AVSynchronizedLayer(playerItem: player.currentItem) {
    syncLayer.frame = CGRect(origin: CGPointZero, size: videoView.bounds.size)
  • 1
    Please post some code for analysis. Sep 12, 2015 at 7:02
  • @SwiftArchitect, thank you for editing. I update my question
    – rkyr
    Sep 14, 2015 at 10:56
  • 1
    Shot in the dark: did you try changing the calculation mode to a linear calculation? Perhaps linear interpolation would offset the difference in frame rates. Sep 16, 2015 at 2:58
  • @TheLivingForce, yes, I already try all types of predefined modes (just in case). I'm tried different predefined types of calculation functions and implement few custom. Code above produce best result.
    – rkyr
    Sep 16, 2015 at 7:33
  • 1
    Have you tried using the customVideoCompositor on your AVPlayerItem?
    – HHK
    Jun 7, 2017 at 22:25


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.