i am new to Swift. I was making an application that can add custom animations and effects like water mark to the video. I have written this code to add a water mark. The problem is that the rectangle for watermark is displayed and image is also displayed but the text is not shown. Here is the code
let videoAsset = AVURLAsset(url: URL(fileURLWithPath: path!))
let mixComposition: AVMutableComposition = AVMutableComposition()
let mutableTrack = mixComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
let assetTrack = videoAsset.tracks(withMediaType: AVMediaTypeVideo)[0]
let timeRangeForVideoAsset = CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
print("1 ...")
do{
try mutableTrack.insertTimeRange(timeRangeForVideoAsset, of: assetTrack, at: kCMTimeZero)
}catch{
print("Error in adding time range")
}
mutableTrack.preferredTransform = assetTrack.preferredTransform
let imageLayer: CALayer = CALayer()
var im = Bundle.main.path(forResource: "Ground", ofType: "png")
let image = UIImage(contentsOfFile: im!)
print("2 ...")
imageLayer.contents = image?.cgImage
imageLayer.opacity = 0.7
imageLayer.frame = CGRect(x: 0, y: 100, width: self.view.frame.width, height: 50)
let videoSize = assetTrack.naturalSize
let parentLayer = CALayer()
let videoLayer = CALayer()
parentLayer.frame = CGRect(x: 0, y: 0, width: assetTrack.naturalSize.width, height: assetTrack.naturalSize.height)
videoLayer.frame = CGRect(x: 0, y: 0, width: assetTrack.naturalSize.width, height: assetTrack.naturalSize.height)
print("3 ...")
let titleLayer = CATextLayer()
titleLayer.backgroundColor = UIColor.white.cgColor
titleLayer.string = "Dummy text"
titleLayer.foregroundColor = UIColor.black.cgColor
titleLayer.font = UIFont(name: "Helvetica", size: 28)
titleLayer.opacity = 1.0
titleLayer.shadowOpacity = 0.5
titleLayer.alignmentMode = kCAAlignmentCenter
titleLayer.frame = CGRect(x: 0, y: 50, width: self.view.frame.width, height: 50)
parentLayer.addSublayer(videoLayer)
parentLayer.addSublayer(imageLayer)
parentLayer.addSublayer(titleLayer)
let videoComp = AVMutableVideoComposition()
videoComp.renderSize = videoSize
videoComp.frameDuration = CMTimeMake(1, 30)
videoComp.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, in: parentLayer)
print("4 ...")
let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, mixComposition.duration)
var layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: assetTrack)
instruction.layerInstructions = [layerInstruction]
videoComp.instructions = [instruction]
print("5 ...")
let assetExport = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)
assetExport?.videoComposition = videoComp
let videoName = "markedVideo.mov"
var exPath = NSTemporaryDirectory().appending(videoName)
if(FileManager.default.fileExists(atPath: exPath)){
do{
try FileManager.default.removeItem(atPath: exPath)
}
catch{
print("File Not Deleted")
}
}
print("6 ...")
assetExport?.outputFileType = AVFileTypeQuickTimeMovie
assetExport?.outputURL = URL(fileURLWithPath: exPath)
assetExport?.shouldOptimizeForNetworkUse = true
assetExport?.exportAsynchronously(completionHandler: {
print("Success ...")
self.playWithMPKit(path: exPath)
})
Please help me what i am doing wrong. I will be thankful to you.
titleLayer.display()
after adjusting the text layer frame