title: Android, How to rotate recorded video using MediaRecorder in portrait mode
body: <p>I am trying to capture video in a custom view. I can display video in correct orientation via Camera.setDisplayOrientation(rotation), but recorded video is still in landscape mode. I cannot capture video correctly in portrait mode.</p>
  
  <p>I tested <a href="https://github.com/googlesamples/android-Camera2Video" rel="nofollow">https://github.com/googlesamples/android-Camera2Video</a>  and it has the same problem too.</p>
  
  <p>I am calling <code>setCameraOrientation()</code> method after <code>Camera.open(currentCameraId)</code> after checking for permissions.</p>
  
  <pre><code>private void setCameraOrientation()
  {
      int rotation = getRotation();
      mCamera.setDisplayOrientation(rotation);
  
      Camera.Parameters parameters = mCamera.getParameters();
      parameters.setRotation(rotation);
      mCamera.setParameters(parameters);
  }
  
  private int getRotation()
  {
      Display display = getWindowManager().getDefaultDisplay();
      int rotation = 0;
      switch (display.getRotation())
      {
          case Surface.ROTATION_0:
              rotation = 90;
              break;
          case Surface.ROTATION_90:
              rotation = 0;
              break;
          case Surface.ROTATION_180:
              rotation = 270;
              break;
          case Surface.ROTATION_270:
              rotation = 180;
              break;
      }
      return rotation;
  }
  </code></pre>
  
  <p>This lets me display video in correct orientation. But when after I stop <code>MediaRecorder</code> and check for saved file, I see 90 degrees rotated video with bad aspect ratio. I checked this via pulling to my computer and in another fragment which has VideoView element. My  prepareRecorder method is: </p>
  
  <pre><code>private boolean prepareRecorder()
  {
      mCamera.lock();
      mCamera.unlock();
      recorder = new MediaRecorder();
  
      recorder.setCamera(mCamera);
  
      recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
  
      CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
      if (!mIsMute)
      {
          recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
          recorder.setProfile(profile);
      }
      else
      {
          recorder.setOutputFormat(profile.fileFormat);
          recorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
          recorder.setVideoFrameRate(profile.videoFrameRate);
          recorder.setVideoEncoder(profile.videoCodec);
          recorder.setVideoEncodingBitRate(profile.videoBitRate);
      }
  
      recorder.setMaxDuration(maxVideoDuration - videoProgress);
  
      String fileName = StorageUtil.sharedUtil().getExternalStorageDir() + File.separator + String.format(
              fileNameFormat, videoCount);
      recorder.setOutputFile(fileName);
  
      recorder.setPreviewDisplay(surfaceHolder.getSurface());
  
      try
      {
          recorder.prepare();
      }
      catch (Exception e)
      {
          e.printStackTrace();
          return false;
      }
  
      return true;
  }
  </code></pre>
  
  <p>and then I just call <code>recorder.start();</code></p>
  
  <p>By the way I tried <code>recorder.setOrientationHint(90);</code> just before <code>recoreder.prepare();</code> but it does not do anything. I can display correct orientation but cannot save.</p>