I'm trying to insert a text layer (CATextLayer
) to a movie. To begin with, let me insert a text layer (CATextLayer
) to a wired NSView
object like
let myLayer = CALayer()
let textLayer = CATextLayer()
let text = "Hello, Mr. Jordan. How are you doing?"
let attributedString = MakeAttributedString.makeRegularCenterAttributedString(text: text, fsize: 64.0, color: NSColor.green) // A class function for creating an NSAttributedString
textLayer.string = attributedString
textLayer.isWrapped = true
textLayer.backgroundColor = NSColor.black.withAlphaComponent(0.9).cgColor
textLayer.cornerRadius = 10.0
textLayer.frame = CGRect(x: 100, y: 200, width: 600, height: 200)
myLayer.addSublayer(textLayer)
myView.layer?.addSublayer(myLayer) // myView is a wired `NSView` object laid over a wired `videoPlayer` object
Okay. I get a CATextLayer
inserted to the view. Now, I try to do the same with AVMutableVideoComposition
, creating a movie with with a CATextLayer
object.
let parentLayer = CALayer()
parentLayer.isHidden = false
parentLayer.opacity = 1.0
parentLayer.frame = CGRect(x: 0, y: 0, width: 1280.0, height: 720.0)
parentLayer.addSublayer(videoLayer)
let myLayer = CALayer()
let textLayer = CATextLayer()
let text = "Hello, Mr. Jordan. How are you doing?"
let attributedString = MakeAttributedString.makeRegularCenterAttributedString(text: text, fsize: 64.0, color: NSColor.green)
textLayer.string = attributedString
textLayer.isWrapped = true
textLayer.backgroundColor = NSColor.black.withAlphaComponent(0.9).cgColor
textLayer.cornerRadius = 10.0
textLayer.frame = CGRect(x: 100, y: 200, width: 600, height: 200)
myLayer.addSublayer(textLayer)
parentLayer.addSublayer(myLayer)
let layerComposition = AVMutableVideoComposition()
layerComposition.frameDuration = CMTimeMake(1, 30)
layerComposition.renderSize = self.frameSize
layerComposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, in: parentLayer)
let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration)
let layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack)
layerInstruction.setTransform(videoTrack.preferredTransform, at: kCMTimeZero)
instruction.layerInstructions = [layerInstruction] as [AVVideoCompositionLayerInstruction]
layerComposition.instructions = [instruction] as [AVVideoCompositionInstructionProtocol]
self.assetExport = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)
self.assetExport.outputFileType = AVFileType.mov
...
...
I get a background color of the text box. But the text string is no show. I can add audio and watermark images (CALayer
) to a movie. But the text string never appears. How come I never end up with a text string?
EDIT
I have tested it under Sierra 10.12.6 with Xcode 9.2. I have also tested it under High Sierra 10.13.4 with Xcode 9.4.1. The text string is no show. CABasicAnimation
won't run with a movie track when the same animation with NSView
works. Setting a text string with NSFont
or with an NSAttributedString
won't make a difference.