java 如何设置 MediaRecorder 以获得最佳的视频质量效果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6058839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:09:35  来源:igfitidea点击:

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

javaandroidencodingcamera

提问by Vivian

Guys can someone tell me how should I set the parameters in the MediaRecorderin 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?

伙计们可以告诉我如何设置参数MediaRecorder才能通过编码获得最佳视频录制效果而不考虑手机的物理限制?还是我的编码造成的视图小失真有什么影响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

如果你们中的一些人可能会猜测不清楚的参数,我实际上是在使用首选项设置一些参数。我错过了哪些可能有助于改进视频编码过程的参数,例如:帧率

回答by Julio Bailon

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

根据 API 级别,您可能希望使用或不使用现有配置文件。

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.

由于并非所有 Android API 和/或设备都支持相同的值,因此您必须查询每个设备的最大值或找到适用于任何地方的东西。

回答by divaPrajapati09

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

使用以下代码获取 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

定义 ORIENTATIONS 如下所示

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.

希望能帮助到你。