Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func mergeVideosFilesWithUrl(savedVideoUrl: URL, newVideoUrl: URL, audioUrl:URL)
- {
- let savePathUrl : NSURL = NSURL(fileURLWithPath: NSHomeDirectory() + "/Documents/camRecordedVideo.mp4")
- do { // delete old video
- try FileManager.default.removeItem(at: savePathUrl as URL)
- } catch { print(error.localizedDescription) }
- var mutableVideoComposition : AVMutableVideoComposition = AVMutableVideoComposition()
- var mixComposition : AVMutableComposition = AVMutableComposition()
- let aNewVideoAsset : AVAsset = AVAsset(url: newVideoUrl)
- let asavedVideoAsset : AVAsset = AVAsset(url: savedVideoUrl)
- let aNewVideoTrack : AVAssetTrack = aNewVideoAsset.tracks(withMediaType: AVMediaType.video)[0]
- let aSavedVideoTrack : AVAssetTrack = asavedVideoAsset.tracks(withMediaType: AVMediaType.video)[0]
- let mutableCompositionNewVideoTrack : AVMutableCompositionTrack = mixComposition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)!
- do{
- try mutableCompositionNewVideoTrack.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: aNewVideoAsset.duration), of: aNewVideoTrack, at: CMTime.zero)
- }catch { print("Mutable Error") }
- let mutableCompositionSavedVideoTrack : AVMutableCompositionTrack = mixComposition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)!
- do{
- try mutableCompositionSavedVideoTrack.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: asavedVideoAsset.duration), of: aSavedVideoTrack , at: CMTime.zero)
- }catch{ print("Mutable Error") }
- let mainInstruction = AVMutableVideoCompositionInstruction()
- mainInstruction.timeRange = CMTimeRangeMake(start: CMTime.zero, duration: CMTimeMaximum(aNewVideoAsset.duration, asavedVideoAsset.duration) )
- let newVideoLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: mutableCompositionNewVideoTrack)
- let newScale : CGAffineTransform = CGAffineTransform.init(scaleX: 0.7, y: 0.7)
- let newMove : CGAffineTransform = CGAffineTransform.init(translationX: 230, y: 230)
- newVideoLayerInstruction.setTransform(newScale.concatenating(newMove), at: CMTime.zero)
- let savedVideoLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: mutableCompositionSavedVideoTrack)
- let savedScale : CGAffineTransform = CGAffineTransform.init(scaleX: 1.2, y: 1.5)
- let savedMove : CGAffineTransform = CGAffineTransform.init(translationX: 0, y: 0)
- savedVideoLayerInstruction.setTransform(savedScale.concatenating(savedMove), at: CMTime.zero)
- mainInstruction.layerInstructions = [newVideoLayerInstruction, savedVideoLayerInstruction]
- mutableVideoComposition.instructions = [mainInstruction]
- mutableVideoComposition.frameDuration = CMTimeMake(value: 1, timescale: 30)
- mutableVideoComposition.renderSize = CGSize(width: 1240 , height: self.camPreview.frame.height)
- finalPath = savePathUrl.absoluteString
- let assetExport: AVAssetExportSession = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)!
- assetExport.videoComposition = mutableVideoComposition
- assetExport.outputFileType = AVFileType.mov
- assetExport.outputURL = savePathUrl as URL
- assetExport.shouldOptimizeForNetworkUse = true
- assetExport.exportAsynchronously { () -> Void in
- switch assetExport.status {
- case AVAssetExportSession.Status.completed:
- print("success")
- case AVAssetExportSession.Status.failed:
- print("failed (assetExport.error)")
- case AVAssetExportSession.Status.cancelled:
- print("cancelled (assetExport.error)")
- default:
- print("complete")
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement