Describe the bug
When using an RTSP stream, the analysis service occasionally crashes with a subprocess.CalledProcessError because sox receives a trim command where the end position is behind the start position.
Error message:
sox FAIL trim: Position 1 is behind the following position.
subprocess.CalledProcessError: Command ['sox', ..., 'trim', '=262.5', '=15'] returned non-zero exit status 2.
Context
- Input type: RTSP Stream
- File:
BirdNET-Pi/scripts/utils/reporting.py
- Function:
extract_safe
Cause
In some cases (likely due to timestamp drift or segment duration mismatches in RTSP streams), the calculated safe_stop ends up being smaller than or equal to safe_start. Since sox uses absolute positioning (with the = prefix), it fails to execute the trim, causing the entire birdnet_analysis.service to crash.
Proposed Fix
I have implemented a safeguard in reporting.py to ensure safe_stop is always at least 3 seconds ahead of safe_start before calling the extract function:
safe_start = max(0, start - spacer)
safe_stop = min(conf.getint('RECORDING_LENGTH'), stop + spacer)
# Prevent sox crash when stop is behind start
if float(safe_stop) <= float(safe_start):
safe_stop = float(safe_start) + 3
extract(in_file, out_file, safe_start, safe_stop)
Describe the bug
When using an RTSP stream, the analysis service occasionally crashes with a
subprocess.CalledProcessErrorbecausesoxreceives a trim command where the end position is behind the start position.Error message:
sox FAIL trim: Position 1 is behind the following position.subprocess.CalledProcessError: Command ['sox', ..., 'trim', '=262.5', '=15'] returned non-zero exit status 2.Context
BirdNET-Pi/scripts/utils/reporting.pyextract_safeCause
In some cases (likely due to timestamp drift or segment duration mismatches in RTSP streams), the calculated
safe_stopends up being smaller than or equal tosafe_start. Sincesoxuses absolute positioning (with the=prefix), it fails to execute the trim, causing the entirebirdnet_analysis.serviceto crash.Proposed Fix
I have implemented a safeguard in
reporting.pyto ensuresafe_stopis always at least 3 seconds ahead ofsafe_startbefore calling theextractfunction: