如何在一个屏幕上合并2个视频?

时间:2017-07-04 07:33:21

标签: swift video merge avasset

我正在制作卡拉OK应用。我想合并用户视频,如卡拉OK视频。屏幕左侧是用户,屏幕右侧是其他用户。我可以合并视频和音频,但我不明白我怎么能合并视频。许多例子都显示合并视频。

func mergeMutableVideoWithAudio(videoUrl:URL, audioUrl:URL){
    let mixComposition : AVMutableComposition = AVMutableComposition()
    var mutableCompositionVideoTrack : [AVMutableCompositionTrack] = []
    var mutableCompositionAudioTrack : [AVMutableCompositionTrack] = []
    let totalVideoCompositionInstruction : AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction()
    //start merge
    let aVideoAsset : AVAsset = AVAsset(url: videoUrl as URL)
    let aAudioAsset : AVAsset = AVAsset(url: audioUrl as URL)
    mutableCompositionVideoTrack.append(mixComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid))
    mutableCompositionAudioTrack.append( mixComposition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid))
    let aVideoAssetTrack : AVAssetTrack = aVideoAsset.tracks(withMediaType: AVMediaTypeVideo)[0]
    let aAudioAssetTrack : AVAssetTrack = aAudioAsset.tracks(withMediaType: AVMediaTypeAudio)[0]
    do{
        try mutableCompositionVideoTrack[0].insertTimeRange(CMTimeRangeMake(kCMTimeZero, aVideoAssetTrack.timeRange.duration), of: aVideoAssetTrack, at: kCMTimeZero)
        try mutableCompositionAudioTrack[0].insertTimeRange(CMTimeRangeMake(kCMTimeZero, aVideoAssetTrack.timeRange.duration), of: aAudioAssetTrack, at: kCMTimeZero)
    }catch{

    }

    mutableCompositionVideoTrack[0].preferredTransform = aVideoAssetTrack.preferredTransform

    totalVideoCompositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero,aVideoAssetTrack.timeRange.duration )
    let mutableVideoComposition : AVMutableVideoComposition = AVMutableVideoComposition()
    mutableVideoComposition.frameDuration = CMTimeMake(1, 30)
    mutableVideoComposition.renderSize = CGSize(width: 480, height: 480)

    mergedAudioVideoURl = NSURL(fileURLWithPath: NSHomeDirectory() + "/Documents/FinalVideo.mp4")
    let assetExport: AVAssetExportSession = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)!
    assetExport.outputFileType = AVFileTypeQuickTimeMovie
    assetExport.outputURL = mergedAudioVideoURl as URL
    removeFileAtURLIfExists(url: mergedAudioVideoURl)
    assetExport.shouldOptimizeForNetworkUse = false

    assetExport.exportAsynchronously { () -> Void in
        switch assetExport.status {
        case AVAssetExportSessionStatus.completed:
            let library = ALAssetsLibrary()
            library.writeVideoAtPath(toSavedPhotosAlbum: self.mergedAudioVideoURl as URL!, completionBlock: { (bool) in
                DispatchQueue.main.async {
                    UserModel.sharedInstance.isVideoMerged = true
                    let effect = self.view.subviews.last
                    effect?.removeFromSuperview()
                    self.popupViewController.dismiss(animated: true, completion: nil)
                }
            })
            print("-----Merge mutable video with trimmed audio exportation complete.\(self.mergedAudioVideoURl)")
        case  AVAssetExportSessionStatus.failed:
            print("failed \(String(describing: assetExport.error))")
        case AVAssetExportSessionStatus.cancelled:
            print("cancelled \(String(describing: assetExport.error))")
        default:
            print("complete")
        }
    }
}

0 个答案:

没有答案