Browse Source

prevent the cache from growing indefinitely

Blake Blackshear 4 years ago
parent
commit
7880d24b29
3 changed files with 23 additions and 3 deletions
  1. 11 0
      config/config.example.yml
  2. 1 1
      detect_objects.py
  3. 11 2
      frigate/events.py

+ 11 - 0
config/config.example.yml

@@ -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

+ 1 - 1
detect_objects.py

@@ -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)

+ 11 - 2
frigate/events.py

@@ -73,6 +73,11 @@ class EventProcessor(threading.Thread):
             earliest_event = min(self.events_in_process.values(), key=lambda x:x['start_time'])['start_time']
         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']:
@@ -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']]