如何转换内置Android摄像机录制的视频的编解码器? [英] How to convert codec of video recorded by inbuilt Android camera?
问题描述
我使用android的内置摄像头录制视频,但是当我通过Retrofit2发送到服务器时,该视频无法在浏览器中看到.因此经过长时间的搜索,我知道我通过android录制的视频编解码器是 MP42
,因此我需要将其转换为 H.264
编解码器,以便使其在浏览器中可播放.
I record video using the inbuilt camera of android,but when I send to server via Retrofit2,the video cannot seen in browser.So after long time of searching,I know that the video that I record by android,the codec is MP42
so I need to convert it to H.264
codec in order to make it playable in browser.
这是我捕获视频的方式
private void recordVideo() {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri();
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);
intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT,15);
startActivityForResult(intent, REQUEST_VIDEO_CAPTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == RECORD_VIDEO_PERMISSION && resultCode == RESULT_OK){
Uri uri =data.getData(); //so the video file I get it here
Log.d("videoData",data.getData().toString());
videoView.setVideoURI(uri);
videoView.start();
}
}
我在 onActivityResult
中获得了视频的uri,视频在 MP42
编解码器中,如何将其转换为 H.264
?
I get the uri of the video in onActivityResult
,the video is in MP42
codec,how can I convert it to H.264
?
我尝试使用 MediaCodec文档,但我不知道在哪里输入我的 Uri
(mp42)以获取H.264编解码器视频(我想在浏览器中播放)的输出.
I tried to use MediaCodec Documentation,but I didnt know where to input my Uri
(mp42) to get the output in H.264 codec video(which I want to play in browser).
请有人给我一些指导.
推荐答案
(1)正在修复当前视频文件:
您的视频使用 Simple
配置文件,格式为 H.263 (或Mpeg-2).正确地说,这意味着您必须转换.您必须使用一些免费的视频工具将此操作重新编码到H.264任务中.
Your video is of format H.263 (or Mpeg-2) using Simple
profile. Correctly as you said, this means you must convert. You must do this re-encode into H.264 task using some free video tool.
例如:使用 手刹 ,您可以...
For example : Using Handbrake you can do...
-
选择打开您的未播放" MP4文件.应该被检测为MP4,现在勾选(或启用)选项
网络优化
.
Choose to Open your "not playing" MP4 file. Should be detected as MP4, now tick (or enable) the option
web optimized
.
在"视频
设置"选项卡中,选择编码器配置文件
"为"主要",选择编码器 level
"为""> 3 .
In Video
settings tab, choose EncoderProfile
as Main and Encoder level
as 3.
在 Destination
中放置您首选的输出文件夹和文件名.
(只需浏览至某个文件夹,然后键入新文件名即可在此处创建).
In Destination
put your preferred output folder and filename.
(just browse to some folder then type your new filename to create here).
单击绿色按钮开始编码
并在浏览器中测试新的MP4输出文件.
Click green button Start Encode
and test new MP4 output file in browser.
(2)为将来的录制修复Android代码:
您必须在 MediaRecorder
对象设置中将编解码器设置为H.264,例如:
You have to set the codec to H.264 in your MediaRecorder
object settings like :
myMediaRec = new MediaRecorder(); //create MediaRecorder object
myMediaRec.setVideoEncoder(MediaRecorder.VideoEncoder.H264); //set video codec
因此,基本上,您的代码应类似于:(未经测试的代码,仅用于学习或指导)...
So basically your code should look like : (untested code, just use for study or guidance)...
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == RECORD_VIDEO_PERMISSION && resultCode == RESULT_OK)
{
//# Create a new instance of MediaRecorder
myMediaRec = new MediaRecorder(); //create MediaRecorder object
mMediaRec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRec.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
myMediaRec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//# Video settings
myMediaRec.setVideoEncoder(MediaRecorder.VideoEncoder.H264); //contained inside MP4
myMediaRec.setVideoSize(640, 480); //width 640, height 480
myMediaRec.setVideoFrameRate(30); //30 FPS
myMediaRec.setVideoEncodingBitRate(3000000); //adjust this for picture quality
//# Audio settings
myMediaRec.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); //must always be AAC
myMediaRec.setAudioEncoder(MediaRecorder.getAudioSourceMax());
myMediaRec.setAudioEncodingBitRate(16);
myMediaRec.setAudioSamplingRate(44100);
}
}
这篇关于如何转换内置Android摄像机录制的视频的编解码器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!