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.
Here is how I capture the video
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();
}
}
I get the uri of the video in onActivityResult
,the video is in MP42
codec,how can I convert it to 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).
Somebody please give me some guidance..
(1) Fixing current video file :
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...
Choose to Open your "not playing" MP4 file. Should be detected as MP4, now tick (or enable) the option web optimized
.
In Video
settings tab, choose EncoderProfile
as Main and Encoder level
as 3.
In Destination
put your preferred output folder and filename.
(just browse to some folder then type your new filename to create here).
Click green button Start Encode
and test new MP4 output file in browser.
(2) Fixing Android code for future recordings :
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);
}
}