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
+2 -3
View File
@@ -171,10 +171,9 @@ audio_offset="0"
cam_audio="$image_dir/sunrise-audio.m4a" cam_audio="$image_dir/sunrise-audio.m4a"
if [ "${AUDIO_ENABLED:-false}" = "true" ]; then if [ "${AUDIO_ENABLED:-false}" = "true" ]; then
if [ -f "$cam_audio" ]; then if [ -f "$cam_audio" ]; then
buffer_sec=$(( ${AUDIO_PRE_BUFFER_MIN:-2} * 60 )) # Already exactly SUNRISE_TARGET_SECS, centred on sunrise — no offset needed
audio_offset=$(echo "scale=1; $SUNRISE_PRE_MIN * 60 + $buffer_sec - $SUNRISE_TARGET_SECS / 2" | bc)
audio_src="$cam_audio" audio_src="$cam_audio"
echo "Audio: camera recording offset=${audio_offset}s" echo "Audio: camera recording (centred on sunrise)"
else else
library_dir="$SCRIPT_DIR/sunrise-sounds" library_dir="$SCRIPT_DIR/sunrise-sounds"
if [ -d "$library_dir" ]; then if [ -d "$library_dir" ]; then
+2 -1
View File
@@ -283,8 +283,9 @@ CAPTURE_INTERVAL=10
# The audio file is deleted automatically after it is mixed into the video. # The audio file is deleted automatically after it is mixed into the video.
# #
AUDIO_ENABLED=false # set true if your camera has a working mic AUDIO_ENABLED=false # set true if your camera has a working mic
AUDIO_PRE_BUFFER_MIN=2 # extra minutes to start before the capture window
CAPTURE_AUDIO_BITRATE=96k # bitrate for the sunrise audio recording CAPTURE_AUDIO_BITRATE=96k # bitrate for the sunrise audio recording
# Audio is recorded for exactly SUNRISE_TARGET_SECS, centred on actual sunrise:
# floor(TARGET/2) seconds before, ceil(TARGET/2) after (odd second → post-sunrise).
# ── Mattermost — daily sunrise upload ───────────────────────────────────────── # ── Mattermost — daily sunrise upload ─────────────────────────────────────────
# mattermost_url, access_token, channel_id go in .env (see bottom of this file). # mattermost_url, access_token, channel_id go in .env (see bottom of this file).
+16 -11
View File
@@ -1,13 +1,15 @@
#!/bin/bash #!/bin/bash
# sunrise-audio-capture.sh — record the camera's RTSP audio stream during the # sunrise-audio-capture.sh — record exactly SUNRISE_TARGET_SECS of audio
# sunrise window so daily_sunrise_video.sh can mix in natural ambient sound # centred on the actual sunrise moment, so daily_sunrise_video.sh can mix in
# (birds, rain, wind) at real speed while the video plays as a timelapse. # real ambient sound (birds, rain, wind) at natural speed.
# #
# Triggered daily at 03:00 by sky-cam-audio-capture.timer. # Triggered at 03:00 by sky-cam-audio-capture.timer; waits internally until
# Waits internally until (sunrise SUNRISE_PRE_MIN AUDIO_PRE_BUFFER_MIN), # the right moment before recording. Exits immediately if AUDIO_ENABLED≠true.
# then records for the full window. 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. # Deleted automatically by daily_sunrise_video.sh after mixing.
set -euo pipefail set -euo pipefail
@@ -40,13 +42,16 @@ fi
sunrise_time_local=$(TZ="$TIMEZONE" date -d "$sunrise_time" +"%H:%M:%S") sunrise_time_local=$(TZ="$TIMEZONE" date -d "$sunrise_time" +"%H:%M:%S")
echo "Sunrise (local): $sunrise_time_local" 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)); } 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") sunrise_sec=$(time_to_seconds "$sunrise_time_local")
buffer_sec=$(( ${AUDIO_PRE_BUFFER_MIN:-2} * 60 )) half_pre=$(( SUNRISE_TARGET_SECS / 2 )) # floor — pre-sunrise portion
record_start_sec=$(( sunrise_sec - SUNRISE_PRE_MIN * 60 - buffer_sec )) half_post=$(( SUNRISE_TARGET_SECS - half_pre )) # ceil — post-sunrise gets odd second
record_dur_sec=$(( (SUNRISE_PRE_MIN + SUNRISE_POST_MIN) * 60 + buffer_sec )) 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) midnight=$(date -d "today 00:00:00" +%s)
now_day_sec=$(( $(date +%s) - midnight )) now_day_sec=$(( $(date +%s) - midnight ))