Change video orientation on Android after capturing video

When we try to record a video using MediaRecorder, the video is recorded correctly in Android and it will appear as recording in the device, but when we can play the video in VLC or other player on the desktop, this time will rotate the video and it will not display correctly ... and I can set MediaRecorder's setOrientationHint to 90 degrees.

what is the problem for changing orientation and why?

+3


source to share


2 answers


We cannot directly apply a fixed orientation when capturing video. What I mean is that you used a fixed 90 degree orientation in the MediaRecorder setOrientationHint. you need to set setOrientationHint (dynamic degree);

First of all, you need to get the display rotation and get the angle using display rotation. after that set the setOrientationHint method for this degree. This will work for everyone. Here is the code.



Display display = getWindowManager().getDefaultDisplay();
int mDisplayRotation = display.getRotation();

public int getDisplayOrientationAngle() {
    Log.e("", "setDisplayOrientationAngle is call");
    int angle;

    // switch (MeasurementNativeActivity.DisplayRotation) {
    switch (mDisplayRotation) {
    case Surface.ROTATION_0: // This is display orientation
        angle = 90; // This is camera orientation
        break;
    case Surface.ROTATION_90:
        angle = 0;
        break;
    case Surface.ROTATION_180:
        angle = 270;
        break;
    case Surface.ROTATION_270:
        angle = 180;
        break;
    default:
        angle = 90;
        break;
    }
    Log.v("", "media recorder displayRotation: " + mDisplayRotation);
    Log.v("", "media recorder angle: " + angle);
    return angle;

}

mMediaRecorder.setOrientationHint(getDisplayOrientationAngle());

      

+1


source


Extracted from Android Documentation for MediaRecorder function setOrientationHint (int degrees) :

This method will not trigger a frame of the original video that will rotate during video recording , but before adding a composition matrix containing the rotation angle in the output video if the output format is OutputFormat.THREE_GPP or OutputFormat.MPEG_4 so that the video player can select the correct orientation for playback .



To summarize, setOrientationHint simply adds some kind of title to the video file that tells the players to rotate the video when it plays . In my experience, VLC player ignores this title and plays the video as it is recorded.

The only workaround I can think of would be to post-process the video by rotating it according to your needs, although that seems like a pretty poor solution in terms of resources.

0


source







All Articles