Search code examples
androidandroid-camera2mediarecordervideo-recording

Android: Camera2 - Reducing delay between video chunks/segments during continuous video recording


I am currently working on an android app that continuously records video using the camera2 API while segmenting it into 1-minute chunks.

My requirement is that the chunks/segments of 1 minute should be seamlessly merged at a later stage.

At the moment I am able to achieve saving in chunks by setting up a timer to restart recording at the end of the time period:

timer.scheduleAtFixedRate(object : TimerTask() {
        override fun run() {
            Log.d(TAG, "Inside timer")
            if (!stopRecording) recordSession() else cancel()
        }
    }, 0, duration)

where duration is the length of the clip that I need. The recording is done using mediaRecorder that outputs to a different file every time it is initialized.

At the end of all this, if I merge the videos there is a noticeable break in the video (few frames lost) and a bit of audio is also skipped, making it quite evident that the video "jumped".

Please let me know how this can be made seamless.

P.S. I have already seen the grafika examples (no audio and it is using camera1 API and not camera2) I've also seen a few SO posts like Android: Split video during capture but the delay is still not solved.


Solution

  • If you need it to be seamless, you need to investigate MediaRecorder's own time limit options, or possibly have to build things from the lower-level API objects of MediaCodec and MediaMuxer.

    Specifically, look at setNextOutputFile and setMaxOutputDuration. That should allow you to automatically transition between files, with a specific length for each snippet. That will hopefully be seamless.

    If not, some combination of a MediaCodec and one or more MediaMuxers should let you build this, but that's a lot of stuff to wire together correctly.