Reputation: 640
I am trying to reduce the size of the video files when using MediaRecorder because I will be streaming the video over a network.
Here is what I have
private boolean prepareMediaRecorder(){
myCamera = getCameraInstance();
mediaRecorder = new MediaRecorder();
myCamera.unlock();
mediaRecorder.setCamera(myCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M
mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}
The CamcorderProfile.QUALITY_LOW line of code seems to make a big difference but nothing else I do has any affect. I tried to reduce the frame rate using setVideoFrameRate and setCaptureRate both have no affect (not sure the difference between the two). I tried to disable the audio source (I don't need audio) by commenting out the setAudioSource line, but that throws an error saying "E/MediaRecorder(7934): try to set the audio encoder without setting the audio source first". The documentation says "If this method is not called, the output file will not contain an audio track".
What parameters should I set in order to keep the file size as low as possible?
Upvotes: 3
Views: 2179
Reputation: 640
Figured it out. The line of code...
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
Sets the output format and video encoder as well as the audio. Remove that line of code as well as set the video encoder and output format to disable audio (Just leave the audio stuff out). This allows you to reduce the file size by a lot.
Upvotes: 3