0

when using AVVideoCompositionCoreAnimationTool to add a text overlay to one of my videos, the first frame is a black one most of the times. When I comment out the all of the code belonging to the animation tool and its setup, everything is working as expected, the first frame is never black. Hence I'm starting to think that my AVMutableComposition used to compose the finished video is indeed working as expected and configured correctly. The following code is used to setup the text layer:

CALayer* layer = [CALayer layer];
layer.frame = CGRectMake(0, 0, 480, 480);
layer.opacity = 0.0f;

CATextLayer* textLayer = [[CATextLayer alloc] init];
textLayer.frame = CGRectMake(0, 0, 450, 80);
[textLayer setString:@"mytext"];
textLayer.foregroundColor = [UIColor whiteColor].CGColor;
textLayer.alignmentMode = kCAAlignmentRight;
[textLayer setFont:CTFontCreateWithName((__bridge CFStringRef)@"HelveticaNeue", 18, &CGAffineTransformIdentity)];
[layer addSublayer:textLayer];

CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];

parentLayer.bounds = CGRectMake(0, 0, 480, 480);
parentLayer.anchorPoint =  CGPointMake(0, 0);
parentLayer.position = CGPointMake(0, 0);

videoLayer.bounds = CGRectMake(0, 0, 480, 480);
[parentLayer addSublayer:videoLayer];
videoLayer.position = CGPointMake(CGRectGetMidX(parentLayer.bounds), CGRectGetMidY(parentLayer.bounds));

textLayer.position = CGPointMake(CGRectGetMidX(layer.bounds), 20);

[parentLayer addSublayer:layer];
videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];

Any ideas on how to fix this issue?

Tim Specht
  • 3,068
  • 4
  • 28
  • 46

2 Answers2

0

Please check this link

http://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos

or

check that you have start video conversation from initial time (0 sec). cause i have same issue with add text to video and i have solved it by changing its initial time.

Hope this will help you.

Urmi
  • 344
  • 1
  • 14
  • I'm actually already using kCMTimeZero. How did you solve the issue? https://gist.github.com/scriptedSheep/f497d61b3267e2dae941 – Tim Specht Sep 15 '14 at 10:20
  • kCMTimeZero is not what you should use; nor should you use 0. Rather: Set animations’ beginTime property to AVCoreAnimationBeginTimeAtZero rather than 0 (which CoreAnimation replaces with CACurrentMediaTime); – James Bush May 17 '16 at 03:43
0

For anyone in the future, here is what I did: Apparently the AVVideoCompositionCoreAnimationTool doesn't play nice with AVMutableVideoCompositions. Therefore I first exported video and audio using an AVAssetExportSession but without my CALayer and afterwards added the overlay layer in a second pass. I know this is probably not a "good" solution and requires a little bit more processing but in the end it worked out.

Tim Specht
  • 3,068
  • 4
  • 28
  • 46
  • Are your creating video file using images or its default video that is already added in resource bundle? – Urmi Sep 16 '14 at 10:03
  • I merge a video and audio track both created by the user. – Tim Specht Sep 16 '14 at 13:19
  • creating video by selecting multiple images? or by recording video with camera? – Urmi Sep 16 '14 at 13:28
  • the user is able to record a video with both a sound and a video track. the sound track is then getting processed and re-added to the video itself to produce the final outcome – Tim Specht Sep 16 '14 at 14:29