Reputation: 11469
I am using AVFoundation
to edit a movie with a custom video compositor, but I can't seem to get this working with core animation. It works fine for playback, but when I try to export, I get unexpected results.
My code looks like this:
AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.instructions = videoInstructions;
videoComposition.frameDuration = CMTimeMake(1, videoTrackA.naturalTimeScale);
videoComposition.renderSize = size;
videoComposition.customVideoCompositorClass = self.project.videoEdits.editClass;
if( isExport )
videoComposition.animationTool = [self createAnimationToolForSize:size];
where createAnimationToolForSize
: looks like this:
-(AVVideoCompositionCoreAnimationTool *) createAnimationToolForSize:(CGSize) size
{
CATextLayer *titleLayer = [CATextLayer layer];
titleLayer.string = @"Text goes here";
titleLayer.font = (__bridge CFTypeRef)(@"Helvetica");
titleLayer.fontSize = size.height / 20;
titleLayer.cornerRadius = size.height / 50;
titleLayer.foregroundColor = [UIColor blackColor].CGColor;
titleLayer.backgroundColor = [UIColor whiteColor].CGColor;
titleLayer.frame = CGRectMake(size.width/4, size.height/4, size.width/2, size.height/2 );
titleLayer.opacity = .5;
//CALayer *parentLayer = [CALayer layer];
//CALayer *videoLayer = [CALayer layer];
//parentLayer.frame = CGRectMake(0, 0, size.width, size.height);
//videoLayer.frame = CGRectMake(0, 0, size.width, size.height);
//[parentLayer addSublayer:videoLayer];
//[parentLayer addSublayer:titleLayer];
//return [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
return [AVVideoCompositionCoreAnimationTool
videoCompositionCoreAnimationToolWithAdditionalLayer:
titleLayer
asTrackID:2];
}
The code as written exports a video with a black background and a rounded white rectangle with no text. The original video, which should be seen in the background, around the edges, is not visible.
If I replace the last line with the commented lines, I simply get the original image with no animation.
Is it possible to get both the movie and the animation (including text)? Am I doing something wrong? Do I need to consider the animation layer in my compositor somehow or is this a bug that prevents you from using core animation
with custom compositors in AVFoundation
?
Upvotes: 1
Views: 1275
Reputation: 11469
I was able to get the animation to display if I create a new track for it, but then my custom renderers need to be able to handle different numbers of tracks (n
when rendering live because the animation is in a CALayer separate from the movie and n+1
when exporting because the layer now exists as a track). That is, of course, doable, even if it is complex, but it leaves some problems unsolved: eg I never figured out why my text doesn't display in my CATextLayer
.
So, even if I did rewrite my code, it's not clear to me that this strategy would work.
Upvotes: 1