Reputation: 29896
I would like to create a video from a number of images and add a cross dissolve effect between the images.
How can this be done? I know images can be written into a video file, but I don't see where to apply an effect. Should each image be turned into a video and then the videos written into the fill video with the transition effect?
I have searched around and cannot find much information on how this can be done, e.g. how to use AVMutableComposition and if it is viable to create videos consisting of individual images, to then apply the cross dissolve effect.
Any information will be greatly appreciated.
Upvotes: 2
Views: 2374
Reputation: 8546
I'm currently doing this right now. To make things short: You need an AVPlayer
to which you put an AVPlayerItem
that can be empty. You then need to set the forwardPlaybackEndTime
of your AVPlayerItem
to the duration of your animation. You then create an AVPlayerLayer
that you initiate with your AVPlayer
. (actually maybe you do not need the AVPlayerLayer
if you will not put video in your animation. Then the important part, you create an AVSynchronizedLayer
that your initiate with your previous AVPlayerItem
. this AVSynchronizedLayer
and any sublayer it holds will be synchronized with your AVPlayerItem
. You can then create some simple CALayer
holding your image (through the contents
property and add your CAKeyframeAnimation
to your layer on the opacity
property. Now any animation on those sublayers will follow the time of your AVPlayerItem
. To start the animation, simply call play
on your AVPlayer
. That's the theory for playback. If you want to export this animation in an mp4 you will need to use AVVideoCompositionCoreAnimationTool
but it's pretty similar.
for code example see code snippet to create animation
Upvotes: 2
Reputation: 1931
If you want to dig around in the bowels of AVFoundation for this, I strongly suggest you take a look at this presentation, especially starting at slide 74 Be prepared to do a large amount of work to pull this off...
If you'd like to get down to business several orders of magnitude faster, and don't mind incorporating a 3rd party library, I'd highly recommend you try GPUImage
You'll find it quite simple to push images into a video and swap them out at will, as well as apply any number of blend filters to the transitions, simply by varying a single mix property of your blend filter over the time your transition happens.
Upvotes: 3