Add ambient chunk as 2nd audio fallback; fix capture service exit-code bug
daily_sunrise_video.sh: - Insert continuous ambient recording extract as 2nd fallback (after today's scheduled sunrise audio, before live RTSP). Finds the chunk file in AUDIO_DIR/<cam>/<date>/ whose start time is the latest one at or before sunrise, then ffmpeg-copies SUNRISE_TARGET_SECS centred on sunrise_sec. - Renumber remaining fallbacks: live RTSP → 3rd, yesterday → 4th, library → 5th. sunrise-audio-capture.sh: - Fix: retention find pipeline could exit non-zero (directory not yet created or permission error) and with set -euo pipefail cause the service to report status=1/FAILURE even after a successful capture. Guard with [ -d ] and add || true so a retention failure never masks a successful recording. https://claude.ai/code/session_01DANuzLCLhhQ7Li12RTcswY
This commit is contained in:
+38
-3
@@ -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
|
||||
|
||||
@@ -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)
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user