6

I have video having duration 4:00. Now I want to add text in video file as per the frames of video. Say for example from 00:30 to 1:50 duration I want to add text "Welcome". Now from 3:00 to 4:00 duration of video I want to add text "Awesome". How to achieve this functionality. I have referred below tutorial. It adds text in whole video not for some duration of video. https://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos

Any help will be appriciated.

I am adding lines of code for add text on whole video:

- (void)applyVideoEffectsToComposition:(AVMutableVideoComposition *)composition size:(CGSize)size
{
    // 1 - Set up the text layer
    CATextLayer *subtitle1Text = [[CATextLayer alloc] init];
    [subtitle1Text setFont:@"Helvetica-Bold"];
    [subtitle1Text setFontSize:36];
    [subtitle1Text setFrame:CGRectMake(0, 0, size.width, 100)];
    [subtitle1Text setString:_subTitle1.text];
    [subtitle1Text setAlignmentMode:kCAAlignmentCenter];
    [subtitle1Text setForegroundColor:[[UIColor whiteColor] CGColor]];

    // 2 - The usual overlay
    CALayer *overlayLayer = [CALayer layer];
    [overlayLayer addSublayer:subtitle1Text];
    overlayLayer.frame = CGRectMake(0, 0, size.width, size.height);
    [overlayLayer setMasksToBounds:YES];

    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:overlayLayer];

    composition.animationTool = [AVVideoCompositionCoreAnimationTool
                                 videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];

}
Payal Maniyar
  • 4,065
  • 1
  • 22
  • 51

2 Answers2

0

Edit Does this answer work for you? You will have to add multiple text layers, and use a CABasicAnimation to show/hide each at the appropriate time (using setBeginTime:).

Original Answer Basically, just maintain a reference to the CATextLayer in this section of code, and use an NSTimer called every second to update your text:

// 1 - Set up the text layer
CATextLayer *subtitle1Text = [[CATextLayer alloc] init]; 
self.textLayer = subtitle1Text; // ### Keep a reference to this object and update it in timerDidFire
...

- (void)viewDidLoad{
    ...
    // Add a timer at some point.  Don't forget to invalidate it later
    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerDidFire) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    ...
}

- (void)timerDidFire{
    if (currentPlaybackPosition <= some_value){
        [self.textLayer setText:@"Welcome"];
    } else if (currentPlaybackPosition <= some_bigger_value){
        [self.textLayer setText:@"Awesome"];
    }
    ...
}
Community
  • 1
  • 1
arsenius
  • 10,530
  • 4
  • 52
  • 69
0

Look at your linked website: https://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos Just use 2 CATextLayer's as shown there. Set their texts.

Set the first ones beginTime property to 30 seconds and its duration property to 80 seconds. The 2nd with a beginTime of 180 seconds and a duration of 60 seconds.

The result will be exported as it is shown in a video player.

Jan
  • 790
  • 7
  • 19
  • Thanks for reply. It shows the text at the given time but it does not disappear after the duration. How to make text disappear? – Payal Maniyar Feb 27 '17 at 06:20
  • The text disappeared always at the end of the duration in my solutions (except if it was too short, meaning far < 1 second). I have no idea why it doesn't work in your code. – Jan Mar 02 '17 at 09:10
  • The code is basically just the code you linked, plus textLayer.beginTime = 30 and textLayer.duration = 80. – Jan Mar 06 '17 at 09:02
  • Hints: Never export on simulator. Always do all animation stuff and exporting purely on the main thread. – Jan Mar 08 '17 at 10:46