Add a text overlay with AVMutableVideoComposition to a specific timerange

前端 未结 2 561
失恋的感觉
失恋的感觉 2020-12-05 21:57

I have found a few examples that show how to add a text overlay on a video.

Ray\'s Tutorial - http://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlay

2条回答
  •  感动是毒
    2020-12-05 22:45

    We should add two opacity animations for Video overlay at particular time rang. First animation for show overlay at start duration and second animation for hide overlay at end duration. We should take care to set fill mode of animation.

    Sample code of Video overlay for particular time Rang.

                let textLayer = CATextLayer()
                textLayer.opacity = 0.0
                .
                .
                textLayer.frame = CGRect(x: 0.0, y: 0.0, width: 150.0, height:100.0)
                textLayer.string = "Overlay Text"
    
                let startVisible = CABasicAnimation.init(keyPath:"opacity")
                startVisible.duration = 0.1    // for fade in duration
                startVisible.repeatCount = 1
                startVisible.fromValue = 0.0  
                startVisible.toValue = 1.0
                startVisible.beginTime = overlay.startTime.seconds // overlay time range start duration
                startVisible.isRemovedOnCompletion = false 
                startVisible.fillMode = kCAFillModeForwards 
                textLayer.add(startVisible, forKey: "startAnimation")
    
                let endVisible = CABasicAnimation.init(keyPath:"opacity")
                endVisible.duration = 0.1
                endVisible.repeatCount = 1
                endVisible.fromValue = 1.0
                endVisible.toValue = 0.0
                endVisible.beginTime = overlay.endTime.seconds
                endVisible.fillMode = kCAFillModeForwards
                endVisible.isRemovedOnCompletion = false
                textLayer.add(endVisible, forKey: "endAnimation")
    

提交回复
热议问题