1616# By default, we set this directory to `/var/lib/motion` in the container, assuming
1717# that the Linux Motion package will also be configured and running in the same container.
1818DATA_MONITORING_PATH = os .environ .get ("DATA_MONITORING_PATH" , "/var/lib/motion" )
19-
19+ MIN_SECONDS_BETWEEN_IMAGES = float ( os . environ . get ( "MIN_SECONDS_BETWEEN_IMAGES" , "2.0" ))
2020
2121def get_socket ():
2222 """
@@ -80,6 +80,35 @@ class NewFileHandler(FileSystemEventHandler):
8080 For now, we are only interested in *new* files, hence, we implement
8181 on_create.
8282 """
83+
84+ def __init__ (self ):
85+ super ().__init__ ()
86+ self .last_image_time = 0
87+
88+ def extract_timestamp (self , file_path ):
89+ basename = os .path .basename (file_path )
90+ try :
91+ # Expected format: 20250714-21:51:49-00.jpg
92+ if "-" in basename and ":" in basename :
93+ # Splitting logic
94+ parts = basename .split ("-" )
95+ if len (parts ) >= 2 and ":" in parts [1 ]:
96+ date_part = parts [0 ]
97+ time_part = parts [1 ].replace (":" , "" )
98+ datetime_str = date_part + time_part
99+ if len (datetime_str ) == 14 :
100+ ts = time .strptime (datetime_str , "%Y%m%d%H%M%S" )
101+ return time .mktime (ts )
102+ except Exception as e :
103+ logging .warning (f"Filename parsing failed: { basename } — { e } " )
104+
105+ # Fallback
106+ try :
107+ return os .path .getmtime (file_path )
108+ except Exception as e :
109+ logging .warning (f"Fallback to time.time(): { file_path } — { e } " )
110+ return time .time ()
111+
83112 def on_closed (self , event ):
84113 """
85114 Watch the directory for new files (not directories), and trigger the
@@ -95,9 +124,17 @@ def process_file(self, file_path):
95124 Basic processing of a new file event.
96125 """
97126 try :
127+ current_time = self .extract_timestamp (file_path )
128+ if current_time - self .last_image_time < MIN_SECONDS_BETWEEN_IMAGES :
129+ logging .info (f"Skipping image (too soon): { file_path } " )
130+ os .remove (file_path )
131+ return
132+
98133 logging .debug (f"Processing file at path: { file_path } " )
99134 uuid = generate_new_image_event (file_path )
100- logging .info (f"Generated uuid ({ uuid } ) and successfully sent new image event for file: { file_path } " )
135+ if uuid :
136+ self .last_image_time = current_time
137+ logging .info (f"Generated uuid ({ uuid } ) and successfully sent new image event for file: { file_path } " )
101138 except Exception as e :
102139 logging .error (f"Error processing { file_path } : { e } " )
103140
0 commit comments