Recording several frames before starting to record

Dear,

I would like to record multiple camera on one event and record the frames before the event.

  1. stream multiple camera
    I am actually streaming two cameras with the help of jetson-inference :
    $ video-viewer /dev/video0 webrtc://@:8554/output
    $ video-viewer /dev/video0 webrtc://@:8555/output

  2. record camera loop
    I am looking to find a way to record both camera with this particularity to save few frames (A) before the event of recording.
    For that, I probably have first to record two loops of (A) frames for both camera. I do not know how to do this.

  3. If an event occurs, break the loop and continue recording camera.

  4. Finally it will results two mpeg4 files.

If someone could help for references or tutorials, thank you,

Sorry to tell don’t have idea, eighter.