我正在开发一个视频应用程序,该应用程序需要在一段特定的时间范围内在视频上显示文本,我需要在不同的时间范围内显示不同的文本。
我已经创建了一个文本层,并将其结合到视频合成中。但是我的问题是我不知道如何根据时间范围更改文本。
这是我到目前为止所做的。
// init variables
let videoAsset: AVAsset = AVAsset(url: videoUrl) as AVAsset
let tracks = videoAsset.tracks(withMediaType: AVMediaType.video)
let videoAssetTrack = tracks.first!
let size = videoAssetTrack.naturalSize
// build video composition
let videoComposition = AVMutableVideoComposition()
videoComposition.renderSize = CGSize(width: size.width, height: size.height)
videoComposition.frameDuration = CMTimeMake(value: 1, timescale: 30)
// create text Layer
let titleLayer = CATextLayer()
titleLayer.backgroundColor = UIColor.yellow.cgColor
titleLayer.foregroundColor = UIColor.black.cgColor
titleLayer.string = "Dummy text"
titleLayer.shadowOpacity = 0.5
titleLayer.alignmentMode = CATextLayerAlignmentMode.center
titleLayer.frame = CGRect(x: 0, y: 50, width: size.width, height: size.height / 6)
let videolayer = CALayer()
videolayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
let parentlayer = CALayer()
parentlayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
parentlayer.addSublayer(videolayer)
parentlayer.addSublayer(titleLayer)
let animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videolayer, in: parentlayer)
videoComposition.animationTool = animationTool
// create instructions
let instructionTimeRange = CMTimeRangeMake(start: CMTime.zero, duration: videoAssetTrack.timeRange.duration)
let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = instructionTimeRange
let layerinstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoAssetTrack)
instruction.layerInstructions = NSArray(object: layerinstruction) as! [AVVideoCompositionLayerInstruction]
videoComposition.instructions = [instruction]
// create exporter and export
let exporter = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPresetHighestQuality)
exporter!.videoComposition = videoComposition
exporter!.outputURL = exportUrl
exporter!.outputFileType = AVFileType.mov
exporter!.shouldOptimizeForNetworkUse = true
exporter!.exportAsynchronously() {
DispatchQueue.main.async {
self.exportDidFinish(exporter!)
}
}
我想在视频的y(假设50.0)秒x(让它为30.0)秒的情况下显示“其他文本”。
编辑: 我已经读过这个Add a text overlay with AVMutableVideoComposition to a specific timerange
但是使用CAKeyframeAnimation可以显示或隐藏特定时间范围内的文本,可以使用Animation更改文本,还是需要使用多个文本层来实现这一点。
我将不胜感激任何提示或解决方案。 TIA。