VideoFlint / Cabbage

A video composition framework build on top of AVFoundation. It's simple to use and easy to extend.
MIT License
1.52k stars 221 forks source link

请问,我使用的是AVMutableVideoComposition中的animationTool给添加layer?怎么添加不了贴画和字幕呢?请问各位大佬有解决方案没? #51

Closed JackMayx closed 4 years ago

JackMayx commented 4 years ago

public func buildVideoComposition() -> AVVideoComposition? {

    if let videoComposition = self.videoComposition, !needRebuildVideoComposition {
        return videoComposition
    }
    buildComposition()
    var layerInstructions: [VideoCompositionLayerInstruction] = []
    mainVideoTrackInfo.forEach { info in
        info.info.forEach({ (provider) in
            let layerInstruction = VideoCompositionLayerInstruction.init(trackID: info.track.trackID, videoCompositionProvider: provider)
            layerInstruction.prefferdTransform = info.track.preferredTransforms[provider.timeRange.vf_identifier]
            layerInstruction.timeRange = provider.timeRange
            layerInstruction.transition = provider.videoTransition
            layerInstructions.append(layerInstruction)
        })
    }
    overlayTrackInfo.forEach { (info) in
        let track = info.track
        let provider = info.info
        let layerInstruction = VideoCompositionLayerInstruction.init(trackID: track.trackID, videoCompositionProvider: provider)
        layerInstruction.prefferdTransform = track.preferredTransforms[provider.timeRange.vf_identifier]
        layerInstruction.timeRange = provider.timeRange
        layerInstructions.append(layerInstruction)
    }

    layerInstructions.sort { (left, right) -> Bool in
        return left.timeRange.start < right.timeRange.start
    }

    // Create multiple instructions,each instructions contains layerInstructions whose time range have insection with instruction,layerrinstruction
    // When rendering the frame, the instruction can quickly find layerInstructions
    let layerInstructionsSlices = calculateSlices(for: layerInstructions)
    let mainTrackIDs = mainVideoTrackInfo.map({ $0.track.trackID })
    let instructions: [VideoCompositionInstruction] = layerInstructionsSlices.map({ (slice) in
        let trackIDs = slice.1.map({ $0.trackID })
        let instruction = VideoCompositionInstruction(theSourceTrackIDs: trackIDs as [NSValue], forTimeRange: slice.0)
        instruction.backgroundColor = timeline.backgroundColor
        instruction.layerInstructions = slice.1
        instruction.passingThroughVideoCompositionProvider = timeline.passingThroughVideoCompositionProvider
        instruction.mainTrackIDs = mainTrackIDs.filter({ trackIDs.contains($0) })
        return instruction
    })

    let videoComposition = AVMutableVideoComposition()

    videoComposition.frameDuration = CMTime(value: 1, timescale: 30)
    videoComposition.renderSize = self.timeline.renderSize
    videoComposition.instructions = instructions
    videoComposition.customVideoCompositorClass = VideoCompositor.self
   //我使用的是以下方式  新增的添加字幕和贴画的代码
    let layerTool = SubtitlePlayerLayerTool()
    layerTool.renderSize = self.timeline.renderSize
    layerTool.subtitlesAndStickersModel = compositionSubTitleAndStickerData.0
    videoComposition.animationTool = layerTool.makeAnimationTool()
    self.videoComposition = videoComposition
    return videoComposition
}