prevent the cache from growing indefinitely

This commit is contained in:
Blake Blackshear 2020-09-15 17:39:05 -05:00
parent fdc8bbf72d
commit 7880d24b29
3 changed files with 23 additions and 3 deletions

View File

@ -17,6 +17,17 @@ mqtt:
#################
# password: password # Optional
################
# Global configuration for saving clips
################
save_clips:
###########
# Maximum length of time to retain video during long events.
# If an object is being tracked for longer than this amount of time, the cache
# will begin to expire and the resulting clip will be the last x seconds of the event.
###########
max_seconds: 300
#################
# Default ffmpeg args. Optional and can be overwritten per camera.
# Should work with most RTSP cameras that send h264 video

View File

@ -290,7 +290,7 @@ def main():
camera_process['process'].start()
print(f"Camera_process started for {name}: {camera_process['process'].pid}")
event_processor = EventProcessor(CONFIG['cameras'], camera_processes, '/cache', '/clips', event_queue, stop_event)
event_processor = EventProcessor(CONFIG, camera_processes, '/cache', '/clips', event_queue, stop_event)
event_processor.start()
object_processor = TrackedObjectProcessor(CONFIG['cameras'], client, MQTT_TOPIC_PREFIX, tracked_objects_queue, event_queue, stop_event)

View File

@ -74,6 +74,11 @@ class EventProcessor(threading.Thread):
else:
earliest_event = datetime.datetime.now().timestamp()
# if the earliest event exceeds the max seconds, cap it
max_seconds = self.config.get('save_clips', {}).get('max_seconds', 300)
if datetime.datetime.now().timestamp()-earliest_event > max_seconds:
earliest_event = datetime.datetime.now().timestamp()-max_seconds
for f, data in list(self.cached_clips.items()):
if earliest_event-90 > data['start_time']+data['duration']:
del self.cached_clips[f]
@ -147,7 +152,11 @@ class EventProcessor(threading.Thread):
self.refresh_cache()
save_clips_config = self.config[camera].get('save_clips', {})
save_clips_config = self.config['cameras'][camera].get('save_clips', {})
# if save clips is not enabled for this camera, just continue
if not save_clips_config.get('enabled', False):
continue
# if specific objects are listed for this camera, only save clips for them
if 'objects' in save_clips_config:
@ -158,7 +167,7 @@ class EventProcessor(threading.Thread):
self.events_in_process[event_data['id']] = event_data
if event_type == 'end':
if save_clips_config.get('enabled', False) and len(self.cached_clips) > 0 and not event_data['false_positive']:
if len(self.cached_clips) > 0 and not event_data['false_positive']:
self.create_clip(camera, event_data, save_clips_config.get('pre_capture', 30))
del self.events_in_process[event_data['id']]