SHARE
TWEET

Untitled

a guest Dec 15th, 2018 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. guard let videoTrack = videoAsset.tracks(withMediaType: AVMediaTypeVideo).first else { return }
  2. let videoComposition = AVMutableVideoComposition()
  3. videoComposition.frameDuration = CMTimeMake(1, 30)
  4. videoComposition.renderSize = CGSize(width: videoTrack.naturalSize.height, height: videoTrack.naturalSize.height)
  5.  
  6. let instruction = AVMutableVideoCompositionInstruction()
  7. instruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
  8.  
  9. let transformer = AVMutableVideoCompositionLayerInstruction.init(assetTrack: videoTrack)
  10. let t1 = CGAffineTransform(translationX: videoTrack.naturalSize.height, y: 0)
  11. let t2 = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))
  12. t1.concatenating(t2)
  13. transformer.setTransform(t1, at: kCMTimeZero)
  14.  
  15. instruction.layerInstructions = [transformer]
  16. videoComposition.instructions = [instruction]
  17.  
  18. guard let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first else { return }
  19. let exportPath = documentPath + "/" + defineFileName() // THIS IS JUST DEFINE RANDOM NAME
  20. let exportUrl = URL(fileURLWithPath: exportPath)
  21.  
  22. let exporter = AVAssetExportSession.init(asset: videoAsset, presetName: AVAssetExportPresetHighestQuality)
  23. exporter?.videoComposition = videoComposition
  24. exporter?.outputURL = exportUrl
  25. exporter?.outputFileType = AVFileTypeQuickTimeMovie
  26.  
  27. exporter?.exportAsynchronously(completionHandler: { () -> Void in
  28.   let library = ALAssetsLibrary()
  29.   if library.videoAtPathIs(compatibleWithSavedPhotosAlbum: exportUrl) {
  30.     library.writeVideoAtPath(toSavedPhotosAlbum: exportUrl, completionBlock: nil)
  31.   }
  32. })
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