title: Mirroring a front-camera video in iOS
body: <p>I'm working on adding video recording to an app using AVFoundation. I managed to record the video and then display it, but then I realized that (unlike in the preview) front-camera videos aren't mirrored along the vertical axis. It seems like this is the standard behavior but I would like the video to look like the preview. I believe that <code>CGAffineTransform</code> can do this but I'm not sure how to apply it to the video.</p>
  
  <p>This is what I had so far:</p>
  
  <pre><code>extension CameraViewController: AVCaptureFileOutputRecordingDelegate {
      func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
          guard error != nil else {
              print("Error recording movie: \(error!.localizedDescription)")
              return
          }
  
          if self.currentCameraPosition == .front {
              mirrorVideo(outputFileURL)
          }
          performSegue(withIdentifier: "ShowVideo", sender: outputFileURL)
      }
  
      func mirrorVideo(_ outputFileURL: URL){
          var transform: CGAffineTransform = CGAffineTransform(scaleX: -1.0, y: 1.0)
          transform = transform.rotated(by: CGFloat(Double.pi/2))
          // Apply transform
      }
  }
  </code></pre>