I recently posted a question regarding exporting a video with a textOverlay (using AVVideoComposition
). I am trying to apply a CATextLayer
overlay to a video in swift using the following method :
@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)
}
Problem
When I use a textfield
to assign a let subTitle1Text : CATextLayer
I get the desired output video with text on it. However if I was to replace the subTitle1Text.string
property with some static text such as subTitle1Text.string = "Sheep"
I don't have any text on the subtitle.
When I preview this with AVPlayer
I can see the expected text on the video but on saving the video with AVExportSession
no subtitle is added.
So my question is , what is the cause for this unusual behaviour?