SHARE
TWEET

Untitled

a guest Jun 27th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. func mergeVideosFilesWithUrl(savedVideoUrl: URL, newVideoUrl: URL, audioUrl:URL)
  2.     {
  3.         let savePathUrl : NSURL = NSURL(fileURLWithPath: NSHomeDirectory() + "/Documents/camRecordedVideo.mp4")
  4.         do { // delete old video
  5.             try FileManager.default.removeItem(at: savePathUrl as URL)
  6.         } catch { print(error.localizedDescription) }
  7.  
  8.         var mutableVideoComposition : AVMutableVideoComposition = AVMutableVideoComposition()
  9.         var mixComposition : AVMutableComposition = AVMutableComposition()
  10.  
  11.         let aNewVideoAsset : AVAsset = AVAsset(url: newVideoUrl)
  12.         let asavedVideoAsset : AVAsset = AVAsset(url: savedVideoUrl)
  13.  
  14.         let aNewVideoTrack : AVAssetTrack = aNewVideoAsset.tracks(withMediaType: AVMediaType.video)[0]
  15.         let aSavedVideoTrack : AVAssetTrack = asavedVideoAsset.tracks(withMediaType: AVMediaType.video)[0]
  16.  
  17.         let mutableCompositionNewVideoTrack : AVMutableCompositionTrack = mixComposition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)!
  18.         do{
  19.             try mutableCompositionNewVideoTrack.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: aNewVideoAsset.duration), of: aNewVideoTrack, at: CMTime.zero)
  20.         }catch {  print("Mutable Error") }
  21.  
  22.         let mutableCompositionSavedVideoTrack : AVMutableCompositionTrack = mixComposition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)!
  23.         do{
  24.             try mutableCompositionSavedVideoTrack.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: asavedVideoAsset.duration), of: aSavedVideoTrack , at: CMTime.zero)
  25.         }catch{ print("Mutable Error") }
  26.  
  27.         let mainInstruction = AVMutableVideoCompositionInstruction()
  28.         mainInstruction.timeRange = CMTimeRangeMake(start: CMTime.zero, duration: CMTimeMaximum(aNewVideoAsset.duration, asavedVideoAsset.duration) )
  29.  
  30.         let newVideoLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: mutableCompositionNewVideoTrack)
  31.         let newScale : CGAffineTransform = CGAffineTransform.init(scaleX: 0.7, y: 0.7)
  32.         let newMove : CGAffineTransform = CGAffineTransform.init(translationX: 230, y: 230)
  33.         newVideoLayerInstruction.setTransform(newScale.concatenating(newMove), at: CMTime.zero)
  34.  
  35.         let savedVideoLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: mutableCompositionSavedVideoTrack)
  36.         let savedScale : CGAffineTransform = CGAffineTransform.init(scaleX: 1.2, y: 1.5)
  37.         let savedMove : CGAffineTransform = CGAffineTransform.init(translationX: 0, y: 0)
  38.         savedVideoLayerInstruction.setTransform(savedScale.concatenating(savedMove), at: CMTime.zero)
  39.  
  40.         mainInstruction.layerInstructions = [newVideoLayerInstruction, savedVideoLayerInstruction]
  41.  
  42.  
  43.         mutableVideoComposition.instructions = [mainInstruction]
  44.         mutableVideoComposition.frameDuration = CMTimeMake(value: 1, timescale: 30)
  45.         mutableVideoComposition.renderSize = CGSize(width: 1240 , height: self.camPreview.frame.height)
  46.  
  47.         finalPath = savePathUrl.absoluteString
  48.         let assetExport: AVAssetExportSession = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)!
  49.         assetExport.videoComposition = mutableVideoComposition
  50.         assetExport.outputFileType = AVFileType.mov
  51.  
  52.         assetExport.outputURL = savePathUrl as URL
  53.         assetExport.shouldOptimizeForNetworkUse = true
  54.  
  55.         assetExport.exportAsynchronously { () -> Void in
  56.             switch assetExport.status {
  57.  
  58.             case AVAssetExportSession.Status.completed:
  59.                 print("success")
  60.             case  AVAssetExportSession.Status.failed:
  61.                 print("failed (assetExport.error)")
  62.             case AVAssetExportSession.Status.cancelled:
  63.                 print("cancelled (assetExport.error)")
  64.             default:
  65.                 print("complete")
  66.             }
  67.         }
  68.  
  69.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top