AVFoundation Issue With Core Animation on Mac OS X

2019-03-31 05:33发布

问题:

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.

回答1:

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!



回答2:

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.