我最近发布了一个关于使用 textOverlay(使用AVVideoComposition
)导出视频的问题。我正在尝试CATextLayer
使用以下方法将叠加层快速应用于视频:
@IBOutlet weak var subTitle1: UITextField!
func applyVideoEffectsToComposition(_ composition: AVMutableVideoComposition!, size: CGSize) {
// 1 - Set up the text layer
let subTitle1Text = CATextLayer()
subTitle1Text.font = "Helvetica-Bold" as CFTypeRef
subTitle1Text.frame = CGRect(x: 0, y: 0, width: size.width, height: 100)
subTitle1Text.string = subTitle1.text
// subTitle1Text.string = "Sheep" ****No subtitle on output video if replace above line with this one ****
subTitle1Text.alignmentMode = kCAAlignmentCenter
subTitle1Text.foregroundColor = UIColor.white.cgColor
// 2 - The usual overlay
let overlayLayer = CALayer()
overlayLayer.addSublayer(subTitle1Text)
overlayLayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
overlayLayer.masksToBounds = true
// 3 - set up the parent layer
let parentLayer = CALayer()
let videoLayer = CALayer()
parentLayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
videoLayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
parentLayer.addSublayer(videoLayer)
parentLayer.addSublayer(overlayLayer)
composition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, in: parentLayer)
}
问题
当我使用 atextfield
分配 a时let subTitle1Text : CATextLayer
,我得到了带有文本的所需输出视频。但是,如果我要用一些静态文本替换该subTitle1Text.string
属性,例如subTitle1Text.string = "Sheep"
我在字幕上没有任何文本。
当我预览这个时,AVPlayer
我可以在视频上看到预期的文本,但在保存视频时AVExportSession
没有添加字幕。
所以我的问题是,这种异常行为的原因是什么?