Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion server/src/motion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
8 changes: 7 additions & 1 deletion server/test/motion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)");
});
});