美文网首页swift
多视频合成添加背景音乐

多视频合成添加背景音乐

作者: 秋叶红90 | 来源:发表于2019-12-28 20:00 被阅读0次

首先创建工具类YSHCaptureUtil

public protocol YSHCaptuerDelegate:NSObjectProtocol {
    func mergeVideoFinsh(exportPath:String?,error:Error?) -> Void ;
    
}
 open class func mergeMoreVideo(videoPaths:[String],audioPath:String,target:YSHCaptuerDelegate) {
            
            
        let mixComposition = AVMutableComposition();
        //混合视频
        let compositionVideoTrack = mixComposition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)
        
        var oldTrack:AVAssetTrack?
        var allDutation:CMTime = CMTime.zero
        
            for e in videoPaths {
                
                do{
                    let videoUrl = URL.init(fileURLWithPath: e)
                                    
                               
                                    
                                                   let videoAsset = AVURLAsset.init(url: videoUrl)
                    let videoAssets = videoAsset.tracks(withMediaType: AVMediaType.video).first!
                   
                                                   
                    
                    try? compositionVideoTrack?.insertTimeRange(videoAssets.timeRange, of: videoAssets, at: oldTrack?.timeRange.duration ?? CMTime.zero)
                    
                    oldTrack = videoAssets
                    
                    allDutation = CMTimeAdd(allDutation, videoAssets.timeRange.duration)
                   
                    
                    
                }
                
                
                
                

                
            }
        
        
            do {
                let videoUrl = URL.init(fileURLWithPath: audioPath)
                let videoAsset = AVURLAsset.init(url: videoUrl)
                let videoAssets = videoAsset.tracks(withMediaType: AVMediaType.audio)
                let compositionVideoTrack = mixComposition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid)
                try? compositionVideoTrack?.insertTimeRange(CMTimeRange.init(start: CMTime.zero, duration: allDutation), of: videoAssets.first!, at: CMTime.zero)
                

            }
        
            
        
            self.exoprotWithMix(mixComposition: mixComposition, target: target)
        }
  open class func exoprotWithMix(mixComposition:AVMutableComposition,target:YSHCaptuerDelegate) {
        
        let exportName = "export2.mp4";
        
        
        let _assetExport = AVAssetExportSession.init(asset: mixComposition, presetName: AVAssetExportPresetPassthrough)
            
        let cacheDirc = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, .userDomainMask, true).first
        
        let exportPath = "\(cacheDirc!)/\(exportName)"
         
        let exportUrl = URL.init(fileURLWithPath: exportPath)
        
            if FileManager.default.fileExists(atPath: exportPath)
            {
                try? FileManager.default.removeItem(atPath: exportPath)
            }
        _assetExport?.outputFileType = AVFileType.mov
        
        print("file type \(String(describing: _assetExport?.outputFileType))")
        print("exportName \(exportPath)")
        _assetExport?.outputURL = exportUrl
        
        _assetExport?.shouldOptimizeForNetworkUse = true
            
        _assetExport?.exportAsynchronously(completionHandler: {
            
            print("exportPath  \(exportPath)")
            target.mergeVideoFinsh(exportPath: exportPath, error: nil)
            
        })
        
        
        
    }

然后就可以了

相关文章

网友评论

    本文标题:多视频合成添加背景音乐

    本文链接:https://www.haomeiwen.com/subject/dbteoctx.html