Reorder audio fallback chain: today → live RTSP → yesterday → library

Moves live RTSP capture to 2nd position so current ambient sound is
preferred over a day-old recording when today's scheduled capture is missing.

https://claude.ai/code/session_01DANuzLCLhhQ7Li12RTcswY
This commit is contained in:
Claude
2026-04-30 11:53:57 +00:00
parent c2d4d97361
commit 65b8caf7f9
+24 -22
View File
@@ -343,33 +343,14 @@ else
if [ -f "$cam_audio" ]; then
audio_src="$cam_audio"
echo "Audio: camera recording (centred on sunrise)"
else
# Yesterday fallback before library: keeps the clip on-site
yesterday_date=$(date -d "yesterday" +%Y-%m-%d)
yday_audio="${AUDIO_DIR:-$BASE_DIR/audio}/sunrise/$CAM_NAME/$yesterday_date/sunrise-audio.m4a"
[ ! -f "$yday_audio" ] && yday_audio="$BASE_DIR/$CAM_NAME/$yesterday_date/sunrise-audio.m4a"
if [ -f "$yday_audio" ]; then
audio_src="$yday_audio"
echo "Audio: yesterday's recording ($yesterday_date)"
else
library_dir="$SCRIPT_DIR/sunrise-sounds"
if [ -d "$library_dir" ]; then
random_file=$(find "$library_dir" -name "*.mp3" | shuf -n 1 2>/dev/null || true)
if [ -n "$random_file" ]; then
audio_src="$random_file"
echo "Audio: library fallback $(basename "$audio_src")"
fi
fi
fi
fi
# Live RTSP as last resort — captures current ambient sound when no
# pre-recorded file is available from today, yesterday, or the library.
# 2nd fallback: live RTSP — captures current ambient sound right now
if [ -z "$audio_src" ]; then
_rtsp_var="CAM_RTSP_${CAM_NAME}"
_rtsp_url="${!_rtsp_var:-}"
if [ -n "$_rtsp_url" ]; then
temp_live_audio=$(mktemp --suffix=.m4a)
echo "Audio: no saved file found — capturing ${SUNRISE_TARGET_SECS}s live from RTSP..."
echo "Audio: no saved file — capturing ${SUNRISE_TARGET_SECS}s live from RTSP..."
if ffmpeg -loglevel warning \
-rtsp_transport tcp \
-i "$_rtsp_url" \
@@ -380,7 +361,28 @@ else
echo "Audio: live RTSP captured OK"
else
rm -f "$temp_live_audio"; temp_live_audio=""
echo "Audio: live RTSP capture failed — continuing silent"
echo "Audio: live RTSP capture failed — trying yesterday"
fi
fi
fi
# 3rd 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"
[ ! -f "$yday_audio" ] && yday_audio="$BASE_DIR/$CAM_NAME/$yesterday_date/sunrise-audio.m4a"
if [ -f "$yday_audio" ]; then
audio_src="$yday_audio"
echo "Audio: yesterday's recording ($yesterday_date)"
fi
fi
# 4th fallback: library
if [ -z "$audio_src" ]; then
library_dir="$SCRIPT_DIR/sunrise-sounds"
if [ -d "$library_dir" ]; then
random_file=$(find "$library_dir" -name "*.mp3" | shuf -n 1 2>/dev/null || true)
if [ -n "$random_file" ]; then
audio_src="$random_file"
echo "Audio: library fallback $(basename "$audio_src")"
fi
fi
fi