Skip to content

Stop the motion detector from filling timestamp gaps with frames - #9

Open
hyngsk wants to merge 1 commit into
codextde:mainfrom
hyngsk:fix/motion-timestamp-jump
Open

hyngsk wants to merge 1 commit into
codextde:mainfrom
hyngsk:fix/motion-timestamp-jump

Conversation

@hyngsk

@hyngsk hyngsk commented Nov 24, 2026

Copy link
Copy Markdown

Problem

The motion detector samples with fps=2. When the stream's timestamps jump forward, fps=2 fills the gap with duplicate frames. That happens when the camera's RTSP server restarts or go2rtc reconnects, because the RTP clock then starts from a new base.

On my camera, that one ffmpeg process grew to 6 GB in about ten minutes after a reconnect. The 4 GB container went into swap and the whole NVR stopped responding for 20 minutes, including recording, live view and the API. The supervisor's stall check could not help, because it was starved too.

A jump backwards has the opposite effect. fps=2 drops every frame until the clock catches up, so detection goes silent.

Reproduction

I used ffmpeg 9.0.2 from the image on a 10 s 1080p clip. A setpts placed before the detector's filter chain makes the timestamps jump by 6 h. Memory was capped with ulimit -v 1500000.

Jump fps=2 (before) select (this PR)
none 20 frames, 47 MB 20 frames, 44 MB
+6 h 13,335 frames, 1.3 GB, then out of memory 20 frames, 44 MB
−6 h 10 frames (half lost) 20 frames, 44 MB

Change

The detector now samples with select. A frame is kept when at least 0.5 s has passed since the last kept frame, or when time went backwards. This filter only drops frames, so a jump costs nothing, and the rate stays at about 2 fps.

The tracker counts hits by wall-clock time and ignores scores below the threshold. The duplicate frames fps=2 used to add had a score of about 0, so dropping them does not change event detection. This also holds in keyframes-only mode.

All tests pass (bun test: 74 tests, including a new one for the sampling filter).

The detector sampled with fps=2. When the stream's timestamps jump
forward - the camera's RTSP server restarts or go2rtc reconnects and the
RTP clock starts from a new base - fps=2 fills the gap with duplicate
frames. A 6 h jump produced 13k frames in seconds and grew the ffmpeg
process to 1.3 GB; on a real camera it grew to 6 GB in about ten minutes
and pushed the host into swap until everything stalled. A jump backwards
made fps=2 drop every frame until the clock caught up, so detection went
silent.

Sample with select instead: keep a frame when at least 0.5 s has passed
since the last kept one, or when time went backwards. It only drops
frames, so a jump costs nothing, and the rate stays at about 2 fps.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant