0

I am trying to create a dashcam from an RTSP streaming device using ffmpeg (in Python). I have it working, but I am losing a small bit of video between stopping a file and starting a new file.

My pseudocode looks like this:

Start recording in 5s increments
while True:
   now = get current time
   if now > split:
       stop recording
       start recording with new file name

At any point when I send a trigger, I wait (up to 5s) for the current file to be written to disk, and then I stitch the required number of files to get a video of the desired length. The issue is that I am losing time between the stop and the start.

I am starting a new file like so:

process_cmd = "ffmpeg -y -loglevel panic -i rtsp://{}:{}@{} -vf scale={}:{} -t {} {}".format(self._rtsp_login, self._rtsp_pwd, self._rtsp_server, self._width, self._height,self._rtsp_video_duration,output_file)
self._process_handle = subprocess.Popen(process_cmd, shell=True)

I am waiting for the video to be done recording by:

self._process_handle.wait()

What's a cleaner way to do this? One option would be to write a longer file and then use ffmpeg to extract the desired N seconds from it.

proc = subprocess.Popen("ffmpeg -y -i rtsp://admin:ambi1234@192.168.1.200 -profile:v high -pix_fmt nv12 -b:v 15M -acodec aac out.mp4", stdin=subprocess.PIPE,shell=True)

This requires me to stop the recording first by sending 'q' to the process and then:

subprocess.Popen("ffmpeg -y -sseof -00:00:3 -i out.mp4 -vcodec copy -acodec copy test.mp4", shell=True)

But that source file (out.mp4) would have to be cleaned up every so often and if an event happens at that point, then I won't be able to capture the data.

trycatch22
  • 307
  • 4
  • 14
  • 1
    You can use the [segment muxer](https://ffmpeg.org/ffmpeg-formats.html#segment) with the `-segment_wrap` option. See [Trim (remove frames from) live video from webcam using python](https://stackoverflow.com/a/63835686/), [FFmpeg Copy Live Stream (Limit to 60s file)](https://stackoverflow.com/q/58909322/), [Stream MJPEG to file, but keep only last x minutes](https://stackoverflow.com/q/34473257/) – llogan Jun 25 '21 at 23:17
  • Thank you!! These were very useful. – trycatch22 Jun 26 '21 at 19:55

0 Answers0