1

I'm using the following code to record the camera in Android. How can I change the default codec (the default is H264) here?

private Uri fileUri;
//...
        private void recordVideo() {
            Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
            // set video quality
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
    
            // start the video capture Intent
            startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
        }
    
    // ...
    
    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE && resultCode == RESULT_OK) {
                // play the video given the global fileUri 
    }
Tina J
  • 4,983
  • 13
  • 59
  • 125

1 Answers1

1

You cannot achieve this with ACTION_VIDEO_CAPTURE intent. You have to open the camera in your app and handle recording video by yourself. The official example using CameraX library are a good starting point.

To control the codec, you need an extra step. You can borrow from this answer, which enforces h264 – but you are free to choose MediaCodec.createEncoderByType() the way you like.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Isn't there "any" ways to do it? (non-safe way is fine). I noticed something like `intent.putExtra(MediaStore.MediaColumns.BITRATE,"30.00");` to set bitrates. Not sure if it works though. – Tina J Dec 13 '21 at 22:03
  • + I added my code. Can you please specify the exact changes in my code? I also saw we can use `mediaRecorder` to achieve this. – Tina J Dec 13 '21 at 22:17
  • 1
    As I say, the change is substantial, because the Intent action cannot be used. There are quite [a few examples also on the SO](https://stackoverflow.com/questions/1817742/how-can-i-record-a-video-in-my-android-app). I am not aware of any device that has non-standard *extras* for camera that control the choice of video codec. – Alex Cohn Dec 14 '21 at 09:23