0

I have a requirement where I have to record video and save video in to chunk of files. How can I split video to multiple file while recording with out loosing video frame

BalaramNayak
  • 1,295
  • 13
  • 16

3 Answers3

6

It is possible if you use the Camera2 API, video and audio codecs, and the MediaMuxer. Basically, the codecs send buffers of data to a MediaMuxer which creates and writes to an .mp4 file. At any point you can start writing the buffers to a different MediaMuxer thus starting a new file. The previous MediaMuxer can then close and save its file. There is a lot of documentation to read and some difficulties. For example, a .mp4 file must start with a key frame. It is possible to request a key frame from the codec, but it won't happen right away, so you must wait for the key frame before starting a new file. You of course must keep track of how must data you have written to files. If your files have some kind of size limit, you must request a key frame before you reach that limit to allow room for frames that will arrive before the key frame appears. And it goes on, but depending on your app requirements, you can get it to work pretty good.

sidetrail
  • 79
  • 2
  • 6
  • Thanks for the answer. How to get keyframe can you provide some code related to the above? – BalaramNayak May 14 '19 at 05:15
  • @BalaramNayak About the key frame, the MediaCodec class has a method called setParameters() that takes a Bundle as an argument. The Bundle contains instructions and settings for the codec. The MediaCodec class provides a bunch of constants to use with this method. So for this case: Bundle bundle = new Bundle(); bundle.putInt( MediaCodec.PARAMETER_KEY_REQUEST_SYNC_FRAME, 0); myCodec.setParameters(bundle); Zero is the only valid value for this parameter. I'll see about putting together some sample code that would be short enough to post. – sidetrail May 15 '19 at 19:26
1

It's no possible record continuous videos without time gaps between them, because to record the MediaRecorder must access camera hardware and use internally Camera.unlock() and Camera.lock() to control access.

The better approach is to record videos in a loop (for one thread) , or use two threads with semaphore implementation gaining near 50ms of time...

Generally the time gap is between 1 and 2 seconds.

If u want more details comment my post and i can give u more details...

1

Take a look at this answer. It's a working sample which is recording camera in segmented files.

Mir Milad Hosseiny
  • 2,769
  • 15
  • 19