How to add text for video duration in iOS SDK

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

Any help would be assigned.

I am adding lines of code to add text to all videos:

- (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]; } 
+5
source share
2 answers

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

Original answer Basically, just keep the CATextLayer link in this section of code and use NSTimer each time 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"]; } ... } 
0
source

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

Set the first beginTime properties to 30 seconds and its duration property to 80 seconds. The second with beginTime 180 seconds and a duration 60 seconds.

The result will be exported as shown on the video player.

0
source

Source: https://habr.com/ru/post/1264329/


All Articles