How to set the MediaRecorder to get the best video quality effect?

24,159

Solution 1

Depending on the API level you may want to use existing profiles or not.

Without profiles:

recorder.setVideoSize(640, 480);
recorder.setVideoFrameRate(16); //might be auto-determined due to lighting
recorder.setVideoEncodingBitRate(3000000);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);// MPEG_4_SP
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

Or if you want to use existing profiles

CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);

Please note that you cannot have both options together as you will get errors or your prepare will not work

As not all the Android API and/or devices support the same values you will either have to query the maximum values per device or find something that works everywhere.

Solution 2

Although the question is quite old, i would like to to point out the combination i used to record the video of HD quality.

Use below combination of the code to achieve HD quality video.

CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);                    
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);                        
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);                        
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mMediaRecorder.setVideoSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);                    
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);                    
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);                    
mMediaRecorder.setVideoEncodingBitRate(cpHigh.videoBitRate);                    
mMediaRecorder.setVideoFrameRate(cpHigh.videoFrameRate);
int rotation = mWindowManager.getDefaultDisplay().getRotation();
int orientation = ORIENTATIONS.get(rotation + 90);
mMediaRecorder.setOrientationHint(orientation);

Use below code to get the DISPLAY_HEIGHT,DISPLAY_WIDTH

DisplayMetrics metrics = new DisplayMetrics();      
mWindowManager.getDefaultDisplay().getMetrics(metrics);          
DISPLAY_WIDTH = metrics.widthPixels;
DISPLAY_HEIGHT = metrics.heightPixels;

Define ORIENTATIONS as shown below

public static final SparseIntArray ORIENTATIONS = new SparseIntArray();
static {
    ORIENTATIONS.append(Surface.ROTATION_0, 90);
    ORIENTATIONS.append(Surface.ROTATION_90, 0);
    ORIENTATIONS.append(Surface.ROTATION_180, 270);
    ORIENTATIONS.append(Surface.ROTATION_270, 180);
}

Hope it helps.

Share:
24,159
Vivian
Author by

Vivian

Updated on August 02, 2020

Comments

  • Vivian
    Vivian over 3 years

    Guys can someone tell me how should I set the parameters in the MediaRecorder in order to get the best video recording effect possible through coding without considering the physical limitation of the phone? Or is there any effect of the view small distortion caused by my coding of the MediaRecorder?

    If some of you might be guessing of the unclear parameters I'm actually setting some of the parameters using preferences. What are the parameters I miss which might help to improve the video encoding process Ex: framerate