Skip to content

Fix Hikvision motion clip boundaries and trim quiet tails - #404

Merged
spupuz merged 1 commit into
spupuz:mainfrom
thomasjohansen:fix/hikvision-motion-clip-cleanup
Oct 2, 2026
Merged

spupuz merged 1 commit into
spupuz:mainfrom
thomasjohansen:fix/hikvision-motion-clip-cleanup

Conversation

@thomasjohansen

Copy link
Copy Markdown
Contributor

Why

Follow-up to #391, based on testing the Hikvision ISAPI integration with a real camera.

The observed alert stream repeatedly reported VMD active but did not report inactive during the tested episodes. This is an observation about the tested stream, not an assumption that Hikvision cameras never send an end signal. Previously, stale motion could remain active until another stream chunk arrived. An alarm returning close to expiry could also split one apparent episode into separate clips with overlapping pre-capture footage.

Simply shortening the timeout risks splitting nearby alarms; keeping the writer open avoids the split but records more quiet footage. This change separates those concerns: preserve the continuation window while recording, then conservatively remove an unnecessary quiet tail from the finished clip.

Changes

  • Expire stale ISAPI motion using the configured Motion Gap, with a watcher that also runs while the HTTP stream is quiet. Continue to honor explicit inactive notifications.
  • Keep the same motion episode/writer open through Captured After, allowing a returning active alarm to continue the clip without duplicate initial label notifications.
  • Respect Captured Before for the encoded passthrough pre-buffer, including configuration updates, within the existing 10-second cap. Available keyframes can limit the actual pre-capture duration.
  • Add offline quiet-tail cleanup for new Hikvision motion clips only, enabled by default with a per-camera opt-out in Event Sources. Continuous recordings and other providers are unaffected.
  • Derive new Timeline start/end timestamps from the recording stop time and probed media duration, adjusting the end when a tail is removed rather than treating a delayed movie-end webhook as the recording start.

Conservative cleanup

  • Analyze at most the final 30 seconds at low resolution and 2 fps; remove at most 15 seconds and keep the configured Captured After after the last detected clear movement.
  • Keep the original when the sampled footage is ambiguous, there is insufficient movement evidence, disk space is insufficient, or processing/validation fails.
  • Remux without re-encoding, preserving audio when present. Validate the candidate duration and final-second decoding before atomically replacing the original.
  • Process before thumbnail generation, storage routing/upload, and final Timeline timestamps; use the existing validated storage path and processing semaphore.
  • No extra camera connection, live AI inference, historical clip trimming, or historical file merging.

This is a conservative frame-difference heuristic, not semantic person detection or a guarantee of finding the exact last-motion frame. Low-resolution sampling and excluding the top/bottom overlay bands have limitations; the opt-out allows cameras or scenes that do not suit the heuristic to retain their original clips.

Temporary diagnostic logs, parser counters, and unknown-event debugging instrumentation have been removed from the branch history. Normal error logging remains.

Verification

  • User-confirmed successful Docker rebuild and real-camera playback/Timeline check; useful motion retained.
  • 9 local tail-cleanup tests passed: quiet-tail trimming, ambiguous small late movement, clock-only footage, remux failure fallback/temporary cleanup, audio preservation, short/missing files, provider eligibility, default/opt-out configuration, and saved file size/timestamps.
  • 27 local provider regressions passed, including quiet-stream expiry, grace-period continuation, duplicate-label prevention, pre-buffer configuration, parser safety, and admin-only diagnostics access.
  • Frontend build and focused lint passed during implementation; diff checks clean.
  • On a temporary copy of one downloaded sample, cleanup reduced duration from 18.04s to 15.00s, retaining the configured 5-second post-capture context. The downloaded original was not modified.

The test scripts are in the project's ignored local test directories (scripts/tests/); these results are local verification, not new committed CI coverage. No automated browser/screenshot verification was performed, and broader lighting/weather conditions remain unverified.

@spupuz
spupuz merged commit 2d0e959 into spupuz:main Oct 2, 2026
2 checks passed
@thomasjohansen

Copy link
Copy Markdown
Contributor Author

@spupuz this could be used in other places as well

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.

2 participants