Audio capture records exactly SUNRISE_TARGET_SECS centred on sunrise

Previously sunrise-audio-capture.sh recorded the entire capture window
(70+ minutes) and daily_sunrise_video.sh sought to the right position with
a complex offset calculation.

Now: record floor(TARGET/2) seconds before sunrise and ceil(TARGET/2) after —
odd second goes to post-sunrise.  The file is exactly SUNRISE_TARGET_SECS long
and already centred, so no offset is needed when mixing.

AUDIO_PRE_BUFFER_MIN removed — it was part of the old whole-window scheme
and is no longer meaningful.

https://claude.ai/code/session_01C4jbd3waXG3eKZYbGUjLUQ
This commit is contained in:
Claude
2026-04-20 00:22:35 +00:00
parent be714affc0
commit 4814c9e158
3 changed files with 20 additions and 15 deletions
+16 -11
View File
@@ -1,13 +1,15 @@
#!/bin/bash
# sunrise-audio-capture.sh — record the camera's RTSP audio stream during the
# sunrise window so daily_sunrise_video.sh can mix in natural ambient sound
# (birds, rain, wind) at real speed while the video plays as a timelapse.
# sunrise-audio-capture.sh — record exactly SUNRISE_TARGET_SECS of audio
# centred on the actual sunrise moment, so daily_sunrise_video.sh can mix in
# real ambient sound (birds, rain, wind) at natural speed.
#
# Triggered daily at 03:00 by sky-cam-audio-capture.timer.
# Waits internally until (sunrise SUNRISE_PRE_MIN AUDIO_PRE_BUFFER_MIN),
# then records for the full window. Exits immediately if AUDIO_ENABLED≠true.
# Triggered at 03:00 by sky-cam-audio-capture.timer; waits internally until
# the right moment before recording. Exits immediately if AUDIO_ENABLED≠true.
#
# Output: BASE_DIR/SUNRISE_CAM/YYYY-MM-DD/sunrise-audio.m4a
# Recording window: floor(TARGET/2) seconds before sunrise,
# ceil(TARGET/2) seconds after (odd second goes to post).
#
# Output: BASE_DIR/<cam>/YYYY-MM-DD/sunrise-audio.m4a
# Deleted automatically by daily_sunrise_video.sh after mixing.
set -euo pipefail
@@ -40,13 +42,16 @@ fi
sunrise_time_local=$(TZ="$TIMEZONE" date -d "$sunrise_time" +"%H:%M:%S")
echo "Sunrise (local): $sunrise_time_local"
# ── Calculate wait and record duration ───────────────────────────────────────
# ── Calculate recording window centred on sunrise ─────────────────────────────
time_to_seconds() { IFS=':' read -r h m s <<< "$1"; echo $((10#$h*3600 + 10#$m*60 + 10#$s)); }
sunrise_sec=$(time_to_seconds "$sunrise_time_local")
buffer_sec=$(( ${AUDIO_PRE_BUFFER_MIN:-2} * 60 ))
record_start_sec=$(( sunrise_sec - SUNRISE_PRE_MIN * 60 - buffer_sec ))
record_dur_sec=$(( (SUNRISE_PRE_MIN + SUNRISE_POST_MIN) * 60 + buffer_sec ))
half_pre=$(( SUNRISE_TARGET_SECS / 2 )) # floor — pre-sunrise portion
half_post=$(( SUNRISE_TARGET_SECS - half_pre )) # ceil — post-sunrise gets odd second
record_start_sec=$(( sunrise_sec - half_pre ))
record_dur_sec=$SUNRISE_TARGET_SECS
echo "Audio window: ${half_pre}s before → ${half_post}s after sunrise (${record_dur_sec}s total)"
midnight=$(date -d "today 00:00:00" +%s)
now_day_sec=$(( $(date +%s) - midnight ))