0

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..

ken
  • 2,426
  • 5
  • 43
  • 98
  • mp42 is a container (it is also a MScodec but Doubt this is the case here) - can you share a link to an example video and we may be able to help better – Mick Oct 12 '17 at 11:23
  • this video is MP42 which I direct upload it to server..it just appear like this – ken Oct 12 '17 at 12:07
  • 2
    Use `MediaRecorder` API to capture video. During the setup code you can choose H.264 as codec. – VC.One Oct 13 '17 at 00:58

1 Answers1

3

(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);

    }
}
VC.One
  • 14,790
  • 4
  • 25
  • 57
  • Can you please answer this question? https://stackoverflow.com/questions/70349561/use-mediarecorder-api-instead-of-intent-for-camera-recording-in-android – Tina J Dec 20 '21 at 20:07