将CIFilter应用于视频,然后添加第二首曲目

问题描述

所以我有两个视频资产-一个用于背景,一个用于前景。我要实现的是将CIFilter(Chromakey)应用于前景视频,然后添加背景视频,以便两者重叠。我可以重叠或应用滤镜,但无法将其放在一起。这是我的视频重叠代码

let bgAsset = AVURLAsset(url: URL(fileURLWithPath: Bundle.main.path(forResource: "bg",ofType: "mp4")!))
let fgAsset = AVURLAsset(url: URL(fileURLWithPath: Bundle.main.path(forResource: "fg",ofType: "mp4")!))
    
let mixComposition = AVMutableComposition()
    
let bgTrack = mixComposition.addMutableTrack(withMediaType: .video,preferredTrackID: kCMPersistentTrackID_Invalid)!
let fgTrack = mixComposition.addMutableTrack(withMediaType: .video,preferredTrackID: kCMPersistentTrackID_Invalid)!

try! bgTrack.insertTimeRange(CMTimeRange(start: .zero,duration: bgAsset.duration),of: bgAsset.tracks(withMediaType: .video).first!,at: .zero)
try! fgTrack.insertTimeRange(CMTimeRange(start: .zero,of: fgAsset.tracks(withMediaType: .video).first!,at: .zero)
    
let mainInstruction = AVMutableVideoCompositionInstruction()
mainInstruction.timeRange = CMTimeRange(start: .zero,duration: bgAsset.duration)
    
let firstLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: bgTrack)
let secondLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: fgTrack)
    
mainInstruction.layerInstructions = [secondLayerInstruction,firstLayerInstruction]
    
let videoComposition = AVMutableVideoComposition()
videoComposition.instructions = [mainInstruction]
videoComposition.frameDuration = CMTime(value: 1,timescale: 30)
videoComposition.renderSize = CGSize(width: 640,height: 480)
    
let url = FileManager.default.urls(for: .documentDirectory,in: .userDomainMask)[0].appendingPathComponent("overlapVideo.mov")
    
let exporter = AVAssetExportSession(asset: mixComposition,presetName: AVAssetExportPresetHighestQuality)!
exporter.outputURL = url
exporter.videoComposition = videoComposition
exporter.outputFileType = .mov
exporter.exportAsynchronously {
    print("finished")
}

感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)