我正在使用 AVMutableComposition 编写视频。我需要在不同的时间间隔添加文本叠加。IE。
- 显示字符串“abc”从 0sec 到 2sec 之后隐藏
- 显示字符串“xyz”从 1sec 到 1.5sec 之后隐藏
- 从 2 秒到 5 秒显示字符串“qwe”
我正在使用下面的代码来添加文本覆盖,但它是静态的并且在视频中保持不变。
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)
非常感谢任何帮助。