1

I think what I'm asking about is similar to this ffmpeg post about how to capture a lightning strike (https://trac.ffmpeg.org/wiki/Capture/Lightning).

I have a Raspberry Pi with an IP cam over RTSP, and what I'm wondering is how to maintain a continual 5 second live video buffer, until I trigger a "save" command which will pipe that 5 second buffer to disk, and continue streaming the live video to disk until I turn it off.

Essentially, Pi boots up, this magic black box process starts and is saving live video into a fixed-size, 5-second buffer, and then let's say an hour later - I click a button, and it flushes that 5-second buffer to a file on disk and continues to pipe the video to disk, until I click cancel.

In my environment, I'm able to use ffmpeg, gstreamer, or openRTSP. For each of these, I can connect to my RTSP stream and save it to disk, but I'm not sure how to create this ever-present 5 second cache.

I feel like the gstreamer docs are alluding to it here (https://gstreamer.freedesktop.org/documentation/application-development/advanced/buffering.html?gi-language=c), but I guess I'm just not grokking how the buffering fits in with a triggered save. From that article, I get the impression that the end-time of the video is known in advance (I could artificially limit mine, I guess).

I'm not in a great position to post-process the file, so using something like openRTSP, saving a whole bunch of video segments, and then merging them isn't really an option.

Note: After a successful save, I wouldn't need to save another video for a minute or so, so that 5 second cache has plenty of time to fill back up before the next

This is the closest similar question that I've found: https://video.stackexchange.com/questions/18514/ffmpeg-buffered-recording

SJoshi
  • 1,866
  • 24
  • 47

2 Answers2

0

Hey,

I dont know if you have knowledge about python, but there is a libary called pyav thats a fancy python wrapper/interface for ffmpeg.

There u can just read your frames from an RTSP Source and handle that frames as you want.

Here is just an idea/hack implementaion about that what u describe, you need to design your framebuffer. When u know that u get 25 FPS from your camera than you can restrict the queue size to 125.

import av
import time
import queue
from threading import Thread, Event


class LightingRecorder(Thread):

    def __init__(self, source: str = ""):
        Thread.__init__(self)
        self.source = source
        self.av_instance = None
        self.connected = False
        self.frame_buffer = queue.Queue()
        self.record_event = Event()

    def open_rtsp_stream(self):
        try:
            self.av_instance = av.open(self.source, 'r')
            self.connected = True
            print ("Connected")
        except av.error.HTTPUnauthorizedError:
            print ("aHTTPUnauthorizedError")
        except Exception as Error:
            # Catch other pyav errors if you want, just for example
            print (Error)

    def run(self):
        self.open_rtsp_stream()

        while 1:
            if self.connected:
                for packet in self.av_instance.demux():
                    for frame in packet.decode():
                        if packet.stream.type == 'video':
                            # TODO:
                            # Handle clearing of Framebuffer, remove frames that are older as a specific timestamp
                            # Or calculate FPS per seconds and store that many frames on framebuffer
                            print ("Add Frame to framebuffer", frame)
                            self.frame_buffer.put(frame)

                        if self.record_event.is_set():
                            [frame.to_image().save('frame-%04d.jpg' % frame.index) for frame in self.frame_buffer]
            else:
                time.sleep(10)

LightingRecorder(source='rtsp://root:pass@192.168.1.197/axis-media/media.amp').start()
Christoph
  • 647
  • 5
  • 18
0

iSpy/AgentDVR can do exactly what you want https://www.ispyconnect.com/userguide-recording.aspx:

Buffer: This is the number of seconds of video to buffer in memory. This feature enables iSpy to capture the full event that causes the motion detection event.

Edit: iSpy runs only on Windows unlike AgentDVR which also has versions for Linux/OSX/RPi.

Amr NA
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 08 '22 at 00:46