title: Rotate video 90 degrees in Swift 4
body: <p>I've been trying to search for this all day, but all answers point to older versions of Swift or Obj-C.</p>
  
  <p>I tried layer instructions, but AVMutableComposition has no member instructions. I remember this being really easy with just an affineTransform, but now I no longer know where I found this. </p>
  
  <pre><code>            var mainVideoURL:URL!
  
              let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
              let tempPath = paths[0] + "/mainVideo.mp4"
  
              if(FileManager.default.fileExists(atPath: tempPath)){
                  guard (try? FileManager.default.removeItem(atPath: tempPath)) != nil else {
                      print("remove path failed")
                      self.enableButtons(enabled:true)
                      return
                  }
              }
              mainVideoURL = URL(fileURLWithPath: tempPath)
  
              let firstAsset = AVURLAsset(url: fileURL)
  
              let mixComposition = AVMutableComposition()
  
  
              // repeat video number of times
  
              let videoRepeat = photoVideoRepeats
  
              for i in 0 ... videoRepeat - 1 {
  
                  do {
  
                      try mixComposition.insertTimeRange(CMTimeRangeMake(kCMTimeZero, firstAsset.duration),
                                                         of: firstAsset,
                                                         at: kCMTimeZero + CMTimeMultiply(firstAsset.duration,Int32(i)))
  
  
  
                  } catch _ {
                      print("Failed to load first track")
                  }
  
              }
  
              guard let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality) else { return }
  </code></pre>