Flutter camera - how to process the video file which is currently recording?

86 views Asked by At

So, I need to start streaming the currently recording video. I mean,

  1. Start recording
  2. start streaming to server
  3. stop recording
  4. send the remaining file chunks, because 4g connection is much slower that realtime
  5. wait while all the network requests are finished

We need to process the video frames and provide some analysis (audio is required). I cannot allow myself to lose a single frame from that video (while working in the unstable 4g environment), that's why I cannot use a traditional streaming approach like "send and forget".

It would be easy for me, if the Camera plugin allowed us to access the file which is currently recording, so I can open it as a read stream and do everything I want. But it doesn't.

I found some code from 2020, which tells me that in that days we were able to provide our own path into the cameraController.startVideoRecording(pathToSave) method. Or at least final pathToSave = cameraController.startVideoRecording(), which is also acceptable for me. The problem would be solved:

  1. final pathToSave = cameraController.startVideoRecording();
  2. startStreamingTheFile(pathToSave);

But right now the only way to access the saved file is final filePath = stopVideoRecording();. I cannot wait for the end of recording!

I tried to record the video in chunks:

  1. record a video fragment nearly 2 minutes long
  2. continue recording data into another fragment
  3. send the first fragment
  4. loop

The problem is, Flutter Camera doesn't allow us to have 2 instances CameraController for the same CameraDescription, see SO question here. And there is a noticable gap between calling stopRecording() and actually receiving the file handle (frames are lost). There is also a gap between calling startVideoRecording() and actually starting the video recording, so I cannot use that "start-stop-loop" approach in the middle of my stream.

Now the only approach that is left is the following (too much work, there must be an easier way):

  1. startImageStream(), listen for each frame. Also listen for the microphone stream.
  2. save the frames as images somewhere in the file system
  3. every 2 minutes, concat the images and the microphone output into one video fragment with ffmpeg
  4. send the fragment, loop. Not a single frame is lost.

Many there is some way to access the recorded file BEFORE the end of recording? Or the recorded file becomes the actual file just before the stopRecording()?

0

There are 0 answers