How to properly export CALayer on top of AVMutableComposition with AVAssetExportSession
Asked Answered
A

2

11

I know this question was asked before (for example here and here) but I just can't figure out what I'm doing wrong.

I have an AVMutableComposition that I use to combine some video clips with some CALayers that animate on top of them.

Everything works ok when I take my AVMutableComposition and combine it with an AVSynchronizedLayer for playback inside an AVPlayerLayer. The video comes out correctly and everything is positioned where it should.

My problem is that when I tried to export this thing I tried using AVVideoCompositionCoreAnimationTool instead of AVSynchronizedLayer (that's what the documentation says we should use for export) and an AVAssetExportSession, but something goes wrong because in my exported movie every CALayer has it's coordinate system reversed.

So if during playback the the 0,0 point is in the top-left corner of the screen when I export the movie the 0,0 point is in the bottom-left corner hence my animation go nuts.

I read any possible article about this and I also downloaded AVVideoEdit sample from Apple but I just can't see what is going on...

Abstract answered 19/7, 2011 at 14:52 Comment(3)
If you ever get an answer I'm interested.Hereof
I never found a good answer for this. What I did was just before the export I pass the layer that holds the animation to a custom function I wrote that goes trough all sublayers recursively and changes the coordinates... It's kind of a hack and an ugly solution but it did the trick for me.Abstract
could you provide some code? I'm trying to do the same stuff and can't get it done :(Medrek
A
23

For anyone who has the same problem as I had I've finally found an answer. There is a property of every CALayer called geometryFlipped wich you need to set to YES just before exporting and everything will be ok. More information here.

Abstract answered 18/10, 2011 at 20:43 Comment(1)
why is this necessary?Martial
A
-1
Methods that fixing frames and points
- (CGRect) fixRect:(CGRect)rect inRect:(CGRect)container
{
    CGRect frame = rect;
    frame.origin.y = container.size.height - frame.origin.y - frame.size.height;
    return frame;
}
- (CGPoint) fixPoint:(CGPoint)point fromPoint:(CGSize)containerSize
{
    CGPoint frame = point;
    frame.y = containerSize.height - frame.y;
    return frame;
}
Autogiro answered 5/7, 2012 at 14:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.