3

With Mac OS X 10.7 Apple has introduced AVFoundation and I'm trying to generate a simple quick time movie containing an animated shape. The problem is that the core animation does not render and I end-up having only a blank "black" video. Below is the code that I used. I've tried many variations but alas none of them work. The export status is always "Completed".

The odd thing is that the UI view contains another layer setup with the same way but added to an AVSynchronizedLayer and it displays just fine and I'm able to scrub back and forth in the animation.

// NOTE: composition is an AVMutableComposition containing a single video 
//       track (30s of black in 1280 x 720).

// Create the animated layer
CALayer *renderAnimLayer = [CALayer layer];
renderAnimLayer.frame = CGRectMake(0, 0, 1280, 720);

// -- EDIT: Added animated square (now animates)
renderAnimLayer.backgroundColor = CGColorCreateGenericRGB(0.3, 0.0, 0.0, 0.5);

// -- Removed
// [self setupAnimationOnLayer:renderAnimLayer];

CALayer *square = [CALayer layer];
square.backgroundColor = CGColorCreateGenericRGB(0, 0, 1, 0.8);
square.frame = CGRectMake(100, 100, 100, 100);

[CATransaction begin];
[CATransaction setDisableActions:YES];
[CATransaction setAnimationDuration:30.0];

CABasicAnimation *animation = [CABasicAnimation animation];
animation.fromValue = [NSValue valueWithPoint:square.position];
animation.toValue = [NSValue valueWithPoint:CGPointOffset(square.position, 800, 400)];
animation.removedOnCompletion = NO;
animation.beginTime = AVCoreAnimationBeginTimeAtZero;
animation.duration = 30.0;

[CATransaction commit];

[square addAnimation:animation forKey:@"position"];
[renderAnimLayer addSublayer:square];
// -- End of Edit

// Create a composition
AVMutableVideoCompositionLayerInstruction *layerInstr1 = 
    [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstruction];
layerInstr1.trackID = 2;

AVMutableVideoCompositionInstruction *instr = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instr.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration);
instr.layerInstructions = [NSArray arrayWithObject:layerInstr1];

AVMutableVideoComposition *renderComp = [AVMutableVideoComposition videoComposition];
renderComp.renderSize    = renderAnimLayer.frame.size;
renderComp.frameDuration = CMTimeMake(1, 30); // Normally 1,30
renderComp.instructions  = [NSArray arrayWithObject:instr];
renderComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithAdditionalLayer:renderAnimLayer asTrackID:2];


// Create an export session and export
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:composition presetName:@"AVAssetExportPreset1280x720"];
exportSession.outputURL = [NSURL URLWithString:@"file:///Users/eric/Desktop/toto.mov"];
exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMake(30, 1));
exportSession.shouldOptimizeForNetworkUse = YES;    
exportSession.videoComposition = renderComp;

// Just see how things have finished.
[exportSession exportAsynchronouslyWithCompletionHandler:^() {
    NSLog(@"Export completed with status: %ld", exportSession.status);
}];

// TODO: remove once everything works and objects have been retained.
while (exportSession.progress < 1.0)
    usleep(200000);

Found Cause of the Issue

Thanks to @ChristianK for the pointer. After adding the blue square animation and seeing it work I had to conclude, like ChritianK, that my setupAnimationOnLayer was the issue. At first I thought it was the way I setup key frame animation but that also worked. It turns out that I was using CAShapeLayers and as per this question CAShapeLayers don't render when renderInContext is called, which is what AVExportSession is trying to do in the background I suppose. This would also explain why I don't have that issue when looking at my layer backed view setup with the same call to setupAnimationOnLayer:.

Thanks to both of you for your help.

Community
  • 1
  • 1
aLevelOfIndirection
  • 3,522
  • 14
  • 18
  • I tried out your code and it worked fine. I've set the background color of renderAnimLayer to red and it showed up in the video. Maybe there's something wrong with your setupAnimationOnLayer method. – klaussner Sep 30 '11 at 12:52
  • Oddly, the same setupAnimationOnLayer is called to setup the CALayer that is in a NSView and it works just fine. I'll try the idea suggested by @Mara. – aLevelOfIndirection Sep 30 '11 at 18:13
  • I've changed the background color as well and I do see the layer but the animated objects don't show nor animate. @ChristianK: have you tried adding a simple animated square? – aLevelOfIndirection Sep 30 '11 at 18:24
  • Added the red background and an animated square that won't animate! I've also tried to put the CALayer into an NSView without success. – aLevelOfIndirection Sep 30 '11 at 19:39
  • I didn't add a square to the renderAnimLayer, but animated the renderAnimLayer itself, though. Here is the code that I used: http://pastebin.com/JsHw6dLE Note: It's important to set the beginTime of the animation to 1e-100. You should also change the renderComp.frameDuration back to CMTimeMake(1, 30). – klaussner Oct 01 '11 at 10:20
  • @ChristianK: you might be right about the setupAnimationOnLayer because after having corrected an obvious bug in my blue-square code above I can now generate the video with a moving blue square. My setup method works fine for a normal CALayer that you put into an NSView (by the way AVCoreAnimationBeginTimeAtZero is 1e-100 or something really small but not zero). I'll see if I can find tweak it to make it work with the export session also. – aLevelOfIndirection Oct 01 '11 at 19:53

2 Answers2

1

Partial Solution

The above code now works to export simple layer animations but what it won't do is export custom CALayers the contents of which is determined by drawInContext: or its delegate version drawLayer:inContext:. CAShapeLayer won't export its content and neither will a MyCustomLayer (as far as I could see).

Next step: discover how to export custom layers in an AVExportSession.

aLevelOfIndirection
  • 3,522
  • 14
  • 18
0

Ahhh, you need to use NSView as the basis for your animation layer. CALayers are very quirky beasts, and a lesser-known fact about them is that they have rendering issues that sometime require that the Quicktime component be dragged outside the browser frame before it will initiate.

You will have to do some refactoring, but NSView is absolutely the place to start. Good luck!

IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756