Irshad Mohamed Irshad Mohamed - 7 days ago 7
iOS Question

Adding multiple texts to AVMutableVideoComposition

I am composing a video using AVMutableComposition. I need to add text overlay at different time intervals. i,e.


  1. display string "abc" from 0sec to 2sec hide afterwards

  2. display string "xyz" from 1sec to 1.5sec hide afterwards

  3. display string "qwe" from 2sec to 5sec



I am using the below code to add text overlay but its static and stays through out the video.

let parentLayer = CALayer()
parentLayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
let videoLayer = CALayer()
videoLayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)

let subtitleText = CATextLayer()
subtitleText.font = font
subtitleText.frame = CGRect(x: 0, y: 100, width: size.width, height: 50)
subtitleText.string = "hhh"
subtitleText.alignmentMode = kCAAlignmentCenter
subtitleText.foregroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1).cgColor
subtitleText.displayIfNeeded()


parentLayer.addSublayer(videoLayer)
parentLayer.addSublayer(subtitleText)


Any help is highly appreciated.

Answer

If anyone needs answer for this, am sorry I couldn't find a direct solution, so i did a costly work around.

  • Trim your video according to your text's time ranges
  • Add text to the trimmed videos
  • And merge those pieces back
Comments