4

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.

enter image description here

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
...
...

enter image description here

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.

El Tomato
  • 6,479
  • 6
  • 46
  • 75
  • It's an innocent mistake. Thanks for pointing that out. – El Tomato Jun 17 '18 at 23:04
  • @matt Thanks. I have done that as well. I have exhausted all possibilities. And I have made a bug report on Radur. – El Tomato Jun 18 '18 at 02:23
  • Nevertheless others have done this. Have you tried just copying some working code and running it? (Of course it may be that this feature works only on iOS, but that would be really weird...) – matt Jun 18 '18 at 02:37
  • @matt Yes and no. I have see many topics for iOS. And I have written my macOS code based on those topics. Many of them are written in Objective-C. I have found several for Swift including the one at the following URL. http://www.mikitamanko.com/blog/2017/05/21/swift-how-to-insert-animated-watermark-into-the-video-or-how-to-merge-two-videos/ I haven't tested my code for iOS. There is little difference. – El Tomato Jun 18 '18 at 03:20
  • @ElTomato did you ever figure this out? I'm having the exact same issue and can't find anyone who's figure out how to do this on macOS anywhere. – James Porter Apr 09 '20 at 06:52
  • @James Porter I don't remember what has solved the issue. I remember having contacted TSI at Apple. – El Tomato Apr 10 '20 at 07:35

0 Answers0