diff --git a/daily_sunrise_video.sh b/daily_sunrise_video.sh index 4aaa283..8e1db4d 100755 --- a/daily_sunrise_video.sh +++ b/daily_sunrise_video.sh @@ -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/// 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 diff --git a/sunrise-audio-capture.sh b/sunrise-audio-capture.sh index 610d605..bb539a2 100755 --- a/sunrise-audio-capture.sh +++ b/sunrise-audio-capture.sh @@ -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