title: Swift - Recorded Video is Mirrored on Front Camera - How to flip?
body: <p>I'm trying to mirror the recorded video from a capture session. The video preview for front facing camera shows a mirrored version, however, when I go to save the file and play it back, the captured video is actually mirrored. I'm using Apple's AVCam demo as a reference and can't seem to figure this out! Please help. </p>
  
  <p>I've tried creating an AVCaptureConnection and trying to set the .isVideoMirrored parameter. However, I get this error:</p>
  
  <blockquote>
    <p> cannot be added to the session because the source and destination media types are incompatible'</p>
  </blockquote>
  
  <p>I would have thought mirroring the video would be much easier. I think I may be creating my connection incorrectly. The code below doesn't actually "Add connection" when I call the .canAddConnection check. </p>
  
  <pre><code>var captureSession: AVCaptureSession!
  
  override func viewDidAppear(_ animated: Bool) {
          super.viewDidAppear(animated) 
  
          captureSession = AVCaptureSession()
  //Setup Camera
   if let dualCameraDevice = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .front) {
                  defaultVideoDevice = dualCameraDevice
  
              } else if let frontCameraDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front) {
                  // If the rear wide angle camera isn't available, default to the front wide angle camera.
                  defaultVideoDevice = frontCameraDevice
              }
              guard let videoDevice = defaultVideoDevice else {
                  print("Default video device is unavailable.")
                 // setupResult = .configurationFailed
                  captureSession.commitConfiguration()
                  return
              }
              let videoDeviceInput = try AVCaptureDeviceInput(device: videoDevice)
  
  if captureSession.canAddInput(videoDeviceInput) {
                  captureSession.addInput(videoDeviceInput)
              }
  
  let movieOutput = AVCaptureMovieFileOutput()
  //Video Input variable for AVCapture Connection 
  let videoInput: [AVCaptureInput.Port] = videoDeviceInput.ports
  
  if captureSession.canAddOutput(movieOutput) {
                  captureSession.beginConfiguration()
                  captureSession.addOutput(movieOutput)
                  captureSession.sessionPreset = .medium
  </code></pre>
  
  <p>Then I try to setup the AVCapture connection and try to set the parameters for mirroring. Please tell me if there is an easier way to mirror the output / playback. </p>
  
  <pre><code> avCaptureConnection = AVCaptureConnection(inputPorts: videoInput, output: movieOutput)
  
   avCaptureConnection.isEnabled = true
   //Mirror the capture connection?
   avCaptureConnection.automaticallyAdjustsVideoMirroring = false
   avCaptureConnection.isVideoMirrored = false
   //Check if we can add a connection
   if captureSession.canAddConnection(avCaptureConnection) {
   //Add the connection                  
   captureSession.addConnection(avCaptureConnection)
    }
  
    captureSession.commitConfiguration()
    self.movieOutput = movieOutput
  
   setupLivePreview()
  
          }
    }
  </code></pre>
  
  <p>Somewhere else in the code, connected to an IBAaction, I initialize the recording</p>
  
  <pre><code>// Start recording video to a temporary file.
      let outputFileName = NSUUID().uuidString
      let outputFilePath = (NSTemporaryDirectory() as NSString).appendingPathComponent((outputFileName as NSString).appendingPathExtension("mov")!)
      print("Recording in tap function")
  
      movieOutput.startRecording(to: URL(fileURLWithPath: outputFilePath), recordingDelegate: self)
  </code></pre>
  
  <p>I think I'm using AVCaptureConnection incorrectly, especially because of the error stating media types are incompatible. If there is a proper way to implement this function please do let me know. Also open to hearing suggestions for an easier way to mirror the playback. Thank you!</p>