Merge pull request #58 from outis1one/clDemoe/fix-video-deletion-error-624Gp

Add ambient chunk as 2nd Demoio fallback; fix capture service exit-cod…
This commit is contained in:
Outis
2026-04-30 18:02:33 -04:00
committed by GitHub
2 changed files with 44 additions and 5 deletions
+38 -3
View File
@@ -344,7 +344,42 @@ else
audio_src="$cam_audio"
echo "Audio: camera recording (centred on sunrise)"
fi
# 2nd fallback: live RTSP — captures current ambient sound right now
# 2nd fallback: continuous ambient recording — extract the sunrise window
# Looks in AUDIO_DIR/<cam>/<date>/ for chunk files named HH-MM-SS[.m4a|-tag.m4a]
# and extracts SUNRISE_TARGET_SECS centred on sunrise_sec from the right chunk.
if [ -z "$audio_src" ]; then
_amb_dir="${AUDIO_DIR:-$BASE_DIR/audio}/$CAM_NAME/$current_date"
if [ -d "$_amb_dir" ]; then
_best_chunk=""
_best_sec=-1
while IFS= read -r _f; do
_hms=$(basename "$_f" | cut -c1-8) # first 8 chars = HH-MM-SS
IFS='-' read -r _h _m _s <<< "$_hms"
[[ "$_h" =~ ^[0-9]{2}$ ]] || continue
_cs=$(( 10#$_h * 3600 + 10#$_m * 60 + 10#$_s ))
if [ "$_cs" -le "$sunrise_sec" ] && [ "$_cs" -gt "$_best_sec" ]; then
_best_chunk="$_f"; _best_sec=$_cs
fi
done < <(find "$_amb_dir" -maxdepth 1 -name "*.m4a" 2>/dev/null | sort)
if [ -n "$_best_chunk" ]; then
_offset=$(( sunrise_sec - _best_sec - SUNRISE_TARGET_SECS / 2 ))
[ "$_offset" -lt 0 ] && _offset=0
temp_live_audio=$(mktemp --suffix=.m4a)
echo "Audio: extracting sunrise window from ambient chunk $(basename "$_best_chunk") at +${_offset}s..."
if ffmpeg -loglevel warning \
-ss "$_offset" -i "$_best_chunk" \
-t "$SUNRISE_TARGET_SECS" -vn -c:a copy \
-y "$temp_live_audio"; then
audio_src="$temp_live_audio"
echo "Audio: ambient chunk extracted OK"
else
rm -f "$temp_live_audio"; temp_live_audio=""
echo "Audio: ambient chunk extraction failed"
fi
fi
fi
fi
# 3rd fallback: live RTSP — captures current ambient sound right now
if [ -z "$audio_src" ]; then
_rtsp_var="CAM_RTSP_${CAM_NAME}"
_rtsp_url="${!_rtsp_var:-}"
@@ -365,7 +400,7 @@ else
fi
fi
fi
# 3rd fallback: yesterday's recording
# 4th fallback: yesterday's recording
if [ -z "$audio_src" ]; then
yesterday_date=$(date -d "yesterday" +%Y-%m-%d)
yday_audio="${AUDIO_DIR:-$BASE_DIR/audio}/sunrise/$CAM_NAME/$yesterday_date/sunrise-audio.m4a"
@@ -375,7 +410,7 @@ else
echo "Audio: yesterday's recording ($yesterday_date)"
fi
fi
# 4th fallback: library
# 5th fallback: library
if [ -z "$audio_src" ]; then
library_dir="$SCRIPT_DIR/sunrise-sounds"
if [ -d "$library_dir" ]; then
+6 -2
View File
@@ -86,11 +86,15 @@ size=$(du -h "$output" | cut -f1)
echo "Audio capture complete: $output (${record_dur_sec}s, $size)"
# Rolling retention for sunrise audio clips
# Guard with [ -d ] so find never exits non-zero under set -euo pipefail,
# which would cause the service to report failure even after a clean capture.
retain="${SUNRISE_AUDIO_RETENTION_DAYS:-7}"
if [ "$retain" -gt 0 ]; then
audio_root="${AUDIO_DIR:-$BASE_DIR/audio}/sunrise/$CAM"
deleted=$(find "$audio_root" -name "sunrise-audio.m4a" -mtime +"$retain" -print -delete 2>/dev/null | wc -l)
[ "$deleted" -gt 0 ] && echo "Retention: deleted $deleted sunrise audio clip(s) older than ${retain}d" || true
if [ -d "$audio_root" ]; then
deleted=$(find "$audio_root" -name "sunrise-audio.m4a" -mtime +"$retain" -print -delete 2>/dev/null | wc -l) || true
[ "$deleted" -gt 0 ] && echo "Retention: deleted $deleted sunrise audio clip(s) older than ${retain}d" || true
fi
fi
exit 0