From 4397c9a481246d8ffdb9253eb36c5b325faacea8 Mon Sep 17 00:00:00 2001 From: Hyeong Seok <41688671+hyngsk@users.noreply.github.com> Date: Tue, 24 Nov 2026 20:37:37 +0900 Subject: [PATCH] Stop the motion detector from filling timestamp gaps with frames 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. --- server/src/motion.ts | 8 +++++++- server/test/motion.test.ts | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/server/src/motion.ts b/server/src/motion.ts index 71e2c13..6381489 100644 --- a/server/src/motion.ts +++ b/server/src/motion.ts @@ -95,11 +95,17 @@ export function segmentDecidable(seg: { end: number }, preMs: number, now: numbe return !hasOpenEvent && now > seg.end + preMs + 2000; } +// Keep at most one frame every 0.5 s. Unlike fps=2 this never invents frames: when the +// stream's timestamps jump forward (the camera or go2rtc reconnects and the RTP clock +// restarts), fps=2 fills the gap with duplicates - hours of them - and the process grows +// by gigabytes. A jump backwards resets the spacing instead of stalling detection. +export const MOTION_SAMPLE = "select=isnan(prev_selected_t)+gte(t-prev_selected_t\\,0.5)+lt(t\\,prev_selected_t)"; + export function buildMotionArgs(ffmpeg: string, input: string, keyframesOnly: boolean): string[] { const args = [ffmpeg, "-hide_banner", "-nostats", "-nostdin", "-loglevel", "info", "-threads", "1"]; if (keyframesOnly) args.push("-skip_frame", "nokey"); if (input.startsWith("rtsp")) args.push("-rtsp_transport", "tcp", "-timeout", "15000000"); - args.push("-an", "-i", input, "-vf", `fps=2,scale=320:-2,select=gte(scene\\,0),metadata=print`, "-an", "-f", "null", "-"); + args.push("-an", "-i", input, "-vf", `${MOTION_SAMPLE},scale=320:-2,select=gte(scene\\,0),metadata=print`, "-an", "-f", "null", "-"); return args; } diff --git a/server/test/motion.test.ts b/server/test/motion.test.ts index 1f4be79..052ca00 100644 --- a/server/test/motion.test.ts +++ b/server/test/motion.test.ts @@ -53,7 +53,13 @@ describe("motion", () => { }); test("detector command samples at 2 fps and prints scene scores", () => { const a = buildMotionArgs("ffmpeg", "rtsp://127.0.0.1/c?video", false).join(" "); - expect(a).toContain("fps=2,scale=320:-2,select=gte(scene\\,0),metadata=print"); + expect(a).toContain("gte(t-prev_selected_t\\,0.5)"); + expect(a).toContain(",scale=320:-2,select=gte(scene\\,0),metadata=print"); expect(buildMotionArgs("ffmpeg", "x", true)).toContain("nokey"); }); + test("detector sampling never duplicates frames across timestamp jumps", () => { + const vf = buildMotionArgs("ffmpeg", "x", false).join(" "); + expect(vf).not.toContain("fps="); + expect(vf).toContain("lt(t\\,prev_selected_t)"); + }); });