将多个文本添加到 AVMutableVideoComposition

Adding multiple texts to AVMutableVideoComposition

我正在使用 AVMutableComposition 制作视频。我需要在不同的时间间隔添加文本覆盖。 IE。

  1. 显示字符串 "abc" 从 0 秒到 2 秒之后隐藏
  2. 显示字符串 "xyz" 从 1 秒到 1.5 秒之后隐藏
  3. 显示字符串 "qwe" 从 2 秒到 5 秒

我正在使用下面的代码添加文本叠加层,但它是静态的,并且会一直贯穿整个视频。

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)

非常感谢任何帮助。

如果有人需要回答这个问题,很抱歉我找不到直接的解决方案,所以我做了一个代价高昂的解决方案。

  • Trim 根据您文本的时间范围显示您的视频
  • 向修剪后的视频添加文字
  • 然后将这些片段合并回去