Skip to content

Bug Report: Sox crash in reporting.py due to safe_stop <= safe_start (RTSP streams) #604

@hessel-a

Description

@hessel-a

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions