我一直试图在10秒的视频上叠加文字。但它输出相同的视频而没有叠加。有人能引导我朝着正确的方向前进吗?代码是示例https://www.raywenderlich.com示例
的直接转换 let mixComposition = AVMutableComposition()
let videoTrack = mixComposition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)
let urlpath = Bundle.main.path(forResource: "vidoe1", ofType: ".MOV")
let url = URL(fileURLWithPath: urlpath!)
let videoasset = AVAsset.init(url: url)
do {
try videoTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoasset.duration), of: videoasset.tracks(withMediaType: AVMediaType.video).first!, at: kCMTimeZero)
}
catch {
}
let mainInstruction = AVMutableVideoCompositionInstruction()
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoasset.duration)
let videolayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack!)
mainInstruction.layerInstructions = [videolayerInstruction]
let mainCompositionInst = AVMutableVideoComposition()
mainCompositionInst.renderSize = CGSize(width: (videoTrack?.naturalSize.width)!, height: (videoTrack?.naturalSize.height)!)
mainCompositionInst.instructions = [mainInstruction]
mainCompositionInst.frameDuration = CMTimeMake(1, 30);
let text = CATextLayer()
text.font = UIFont(name: "Helvetica-Bold", size: 30.0)
text.frame = CGRect(x: 0, y: 200, width: (videoTrack?.naturalSize.width)!, height: 100)
text.string = "hello world"
text.foregroundColor = UIColor.black.cgColor
text.alignmentMode = kCAAlignmentCenter
let overlaylayer = CALayer()
overlaylayer.addSublayer(text)
overlaylayer.frame = CGRect(x: 0, y: 0, width: (videoTrack?.naturalSize.width)!, height: (videoTrack?.naturalSize.height)!)
overlaylayer.masksToBounds = true
let parentLayer = CALayer()
let videoLayer = CALayer()
parentLayer.frame = CGRect(x: 0, y: 0, width: (videoTrack?.naturalSize.width)!, height: (videoTrack?.naturalSize.height)!)
videoLayer.frame = CGRect(x: 0, y: 0, width: (videoTrack?.naturalSize.width)!, height: (videoTrack?.naturalSize.height)!)
parentLayer.addSublayer(videoLayer)
parentLayer.addSublayer(overlaylayer)
mainCompositionInst.animationTool = AVVideoCompositionCoreAnimationTool.init(postProcessingAsVideoLayer: overlaylayer, in: parentLayer)
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let path = documentsPath + "FinalVidwdw.mov"
let urlexport = URL(fileURLWithPath: path)
let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)
exporter?.outputURL = urlexport
exporter?.outputFileType = AVFileType.mov
exporter?.shouldOptimizeForNetworkUse = true
exporter?.videoComposition = mainCompositionInst
exporter?.exportAsynchronously(completionHandler: {
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: url)
}) { saved, error in
if saved {
let alertController = UIAlertController(title: "Your video was successfully saved", message: nil, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
else {
print("bad panda")
}
}
}