Android Question Camera2 - Recording Video

biometrics

Active Member
Licensed User
Longtime User
Re: https://www.b4x.com/android/forum/threads/camera2-still-images-and-videos.83920/

1. The sample seems to set the video capture size to 1920x1080 (CaptureSize.Initialize(1920, 1080)) but my resulting file is 640x480. How do I set the video capture size? My phone is a Sony Z3 that supports that resolution.
2. How can I set the video bit rate (e.g. 1000 kbps or 500 kbps)
3. How can I set the video frame rate (e.g. 30 fps or 12 fps)
4. Is possible to add closed captioning to the recording video (subtitles) or alternatively can I overlay text?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. The video size is set in the call to CreateMediaRecorder. In CamEx2 implementation it uses PreviewSize to set the video size.

2. You can customize the returned MediaRecorder object:
https://developer.android.com/reference/android/media/MediaRecorder#setVideoFrameRate(int)
B4X:
Dim FrameRate As Int = 12
MediaRecorder.RunMethod("setVideoFrameRate", Array(FrameRate)) 'set to 30 by default

There is also a setVideoEncodingBitRate method (which is set to 10000000 by default).

4. It is not possible to add subtitles or text to the video itself. You can create a SRT file with the subtitles. You can play videos with subtitles with ExoPlayer.
 
Upvote 0

biometrics

Active Member
Licensed User
Longtime User

I'm having some trouble getting this right. I'm unsure where to put this code.

The documentation states that setVideoFrameRate "Must be called after setVideoSource()". I can't find such a call. It also says that it throws a "IllegalStateException" if "if it is called after prepare() or before setOutputFormat()".

I noticed that setOrientationHint is called in PrepareSurfaceForVideo. The documentation states that "This method should be called before prepare()."

Since setOrientationHint needs to be called before "prepare()" I assumed that would be a good place to call setVideoFrameRate. I added it after

B4X:
MediaRecorder.RunMethod("setOrientationHint", Array(orientations.Get(Floor(rotation / 90))))

but I'm getting an IllegalStateException.

So my question is, where is the right place to call setVideoFrameRate and setVideoEncodingBitRate?
 
Upvote 0

biometrics

Active Member
Licensed User
Longtime User

Seems it's an issue either with Android 5 or the specific device (https://www.viatech.com/en/boards/3-5-inch-sbc/vab-630/) as it works on my Sony Z3 with Android 6.

Setting the bit rate worked fine but the frame rate remained at 25+ frames per second. I found the answer on Stackoverflow, you also need to call setCaptureRate:

B4X:
Dim iFrameRate As Int = 15
Dim dFrameRate As Double = 15
MediaRecorder.RunMethod("setCaptureRate", Array(dFrameRate))
MediaRecorder.RunMethod("setVideoFrameRate", Array(iFrameRate))
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…