Is there a way to save last N seconds of a video stream to a file with openCV? E.g. The camera recording starts at 0s and ends at 20s leading to a recorded file which contains the video from 10s -> 20s.
One way I can think of is to save last N seconds in a memory buffer and write them to file once the process finishes, but this is not a desireable solution because of the latency involved at the end as well as memory limitations when multiple HD streams are involved.
CodePudding user response:
The best solution is to use a fifo buffer for the last 10 seconds or stream and past it into a file when the process stop (as you've explained).
why would it imply a latency though ? just need to use 2 buffer. a short buffer for display and long fifo buffer for recording last 10 sec
CodePudding user response:
Common "dashcam"/CCTV solutions write short video segments continuously, then remove the oldest ones as needed.
You can't have a single file in which data "disappears" again.
Use streamable video container formats like MPEG (1/2) Transport Streams (.ts
or .mts
or various other suffixes). These types can be decoded even if they're incomplete. Do not use containers that must be "finalized" (metadata written) to be decodable, e.g. .mp4
. In any exceptional situation, incomplete files of that type would be unrecoverable.