Files
sky-cam/daily_sunrise_video.sh
T
Claude baf886fd23 Audio resilience: three-tier fallback for sunrise audio; add freesound download script
daily_sunrise_video.sh: priority chain is camera mic → library random MP3 →
overlay-only → sped video promotion.  If audio mixing fails a warning
notification fires and the script retries with overlay-only so a video is
always produced and uploaded.

download-sunrise-sounds.py: downloads CC-licensed ambient sounds from
freesound.org into 11 weather/season category folders (~275 files total at
25 per category), writing a manifest.json with attribution data.

https://claude.ai/code/session_01C4jbd3waXG3eKZYbGUjLUQ
2026-04-19 21:22:01 +00:00

220 lines
9.8 KiB
Bash

#!/bin/bash
# daily_sunrise_video.sh — collect today's sunrise images, speed-adjust to
# SUNRISE_TARGET_SECS, burn the local sunrise time vertically on the right
# side, and write the final video. Upload is handled by a separate systemd
# service (sky-cam-sunrise-upload) triggered via OnSuccess= so the two jobs
# have independent log entries and failure states.
#
# Resilience: the speed-adjusted video is saved permanently before the overlay
# step. If overlay fails, the sped video survives at:
# <output_dir>/<date>-daily-sunrise-sped.mp4
# Re-run this script once the issue is resolved; the sped file is deleted
# automatically when the overlay step succeeds.
set -euo pipefail
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
source "$SCRIPT_DIR/sky-cam.conf"
export TZ="$TIMEZONE"
# ── Date / paths ──────────────────────────────────────────────────────────────
current_date=$(date +%Y-%m-%d)
output_date=$(date +%Y-%m)
image_dir="$BASE_DIR/$CAM_NAME/$current_date"
output_dir="$BASE_DIR/movies/$CAM_NAME/$output_date/sunrise-only"
mkdir -p "$output_dir"
# ── Sunrise time ──────────────────────────────────────────────────────────────
sunrise_time=$(python3 "$SCRIPT_DIR/sunrise.py")
if [[ -z "$sunrise_time" ]]; then
"$SCRIPT_DIR/notify.sh" "FAILED: sunrise $current_date" \
"Could not retrieve sunrise time from sunrise.py" || true
echo "Error: Failed to retrieve sunrise time."
exit 1
fi
echo "Sunrise (UTC): $sunrise_time"
sunrise_time_local=$(TZ="$TIMEZONE" date -d "$sunrise_time" +"%H-%M-%S")
echo "Sunrise (local): $sunrise_time_local"
# ── Capture window ────────────────────────────────────────────────────────────
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")
start_sec=$((sunrise_sec - SUNRISE_PRE_MIN * 60))
end_sec=$((sunrise_sec + SUNRISE_POST_MIN * 60))
echo "Window: $((start_sec/3600)):$(printf '%02d' $(((start_sec%3600)/60)))$((end_sec/3600)):$(printf '%02d' $(((end_sec%3600)/60)))"
# ── Collect images in window ──────────────────────────────────────────────────
if [ ! -d "$image_dir" ]; then
"$SCRIPT_DIR/notify.sh" "FAILED: sunrise $current_date" \
"Image directory not found: $image_dir" || true
echo "Error: image directory not found: $image_dir"
exit 1
fi
temp_list=$(mktemp --suffix=.txt)
raw_video=""
trap 'rm -f "$temp_list" "$raw_video" 2>/dev/null || true' EXIT
find "$image_dir" -type f -name "*.jpg" | sort | while read -r img; do
img_sec=$(time_to_seconds "$(basename "$img" .jpg)")
if [[ $img_sec -ge $start_sec && $img_sec -le $end_sec ]]; then
echo "file '$img'"
fi
done > "$temp_list"
num_images=$(wc -l < "$temp_list")
if [[ $num_images -lt 1 ]]; then
"$SCRIPT_DIR/notify.sh" "FAILED: sunrise $current_date" \
"No images found in sunrise window" || true
echo "No images found in sunrise window."
exit 1
fi
echo "Images in window: $num_images"
# ── Font detection (same fallback chain as montage-mvt.sh) ───────────────────
FONT=""
if command -v fc-match &>/dev/null; then
FONT=$(fc-match "DejaVu Sans:style=Regular" --format="%{file}" 2>/dev/null || true)
fi
if [ -z "$FONT" ]; then
for f in \
/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf \
/usr/share/fonts/dejavu/DejaVuSans.ttf \
/usr/share/fonts/TTF/DejaVuSans.ttf \
/usr/share/fonts/truetype/freefont/FreeSans.ttf; do
[ -f "$f" ] && { FONT="$f"; break; }
done
fi
# ── Step 1: Raw video from images ─────────────────────────────────────────────
raw_video=$(mktemp --suffix=.mp4)
echo "Step 1/3: encoding raw video..."
if ! ffmpeg -loglevel warning \
-f concat -safe 0 -i "$temp_list" \
-c:v libx264 -pix_fmt yuv420p -crf "$CRF_SUNRISE" -vsync 2 -an \
-y "$raw_video"; then
"$SCRIPT_DIR/notify.sh" "FAILED: sunrise step 1/3 (encode) $current_date" \
"ffmpeg raw encode from images failed — check: journalctl -u sky-cam-sunrise.service" || true
exit 1
fi
raw_dur=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$raw_video")
speed_factor=$(echo "scale=3; $raw_dur / $SUNRISE_TARGET_SECS" | bc)
echo "Raw: ${raw_dur}s speed factor: ${speed_factor}x"
# ── Step 2: Speed-adjust — saved permanently so overlay failure is recoverable ─
# Deleted automatically if step 3 succeeds.
sped_video="$output_dir/$current_date-daily-sunrise-sped.mp4"
echo "Step 2/3: speed-adjust → $sped_video"
if ! ffmpeg -loglevel warning \
-i "$raw_video" \
-vf "setpts=PTS/${speed_factor}" \
-c:v libx264 -pix_fmt yuv420p -crf "$CRF_SUNRISE" -an \
-y "$sped_video"; then
rm -f "$sped_video"
"$SCRIPT_DIR/notify.sh" "FAILED: sunrise step 2/3 (speed) $current_date" \
"speed-adjust failed — no video saved" || true
exit 1
fi
sped_dur=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$sped_video")
echo "Speed-adjusted: ${sped_dur}s (saved — overlay still pending)"
# ── Build overlay filter ──────────────────────────────────────────────────────
# Time displayed as stacked characters down the right side (e.g. 0/7/:/2/3),
# semi-transparent with a soft drop shadow so it reads on any background.
SR_TIME=$(echo "$sunrise_time_local" | cut -d'-' -f1,2 | tr '-' ':')
# Build "H\nH\n:\nM\nM" for vertical stacking in drawtext
SR_VERT=$(echo "$SR_TIME" | awk 'BEGIN{FS=""}{for(i=1;i<=NF;i++){printf "%s",$i; if(i<NF)printf "\\n"}}')
DT="drawtext"
[ -n "$FONT" ] && DT="${DT}=fontfile='${FONT}'" || DT="${DT}"
DT="${DT}:text='${SR_VERT}'"
DT="${DT}:fontcolor=white@${SUNRISE_OVERLAY_OPACITY}"
DT="${DT}:fontsize=h/22"
DT="${DT}:line_spacing=4"
DT="${DT}:x=w-tw-18:y=(h-th)/2"
DT="${DT}:shadowcolor=black@0.55:shadowx=1:shadowy=1"
# ── Step 3: Overlay + optional audio ─────────────────────────────────────────
# Priority: (1) camera mic recording, (2) library fallback, (3) no audio.
# If audio mixing fails, step 3 retries with overlay-only so the video is
# always made. If overlay itself fails, the sped video is promoted so upload
# still fires.
final_video="$output_dir/$current_date-daily-sunrise.mp4"
fade_out=$(echo "scale=1; $SUNRISE_TARGET_SECS - 0.5" | bc)
# ── Pick audio source ─────────────────────────────────────────────────────────
audio_src=""
audio_offset="0"
cam_audio="$image_dir/sunrise-audio.m4a"
if [ "${AUDIO_ENABLED:-false}" = "true" ]; then
if [ -f "$cam_audio" ]; then
# Camera recording: offset centres the clip on actual sunrise
buffer_sec=$(( ${AUDIO_PRE_BUFFER_MIN:-2} * 60 ))
audio_offset=$(echo "scale=1; $SUNRISE_PRE_MIN * 60 + $buffer_sec - $SUNRISE_TARGET_SECS / 2" | bc)
audio_src="$cam_audio"
echo "Audio: camera recording offset=${audio_offset}s"
else
# Library fallback: random file from sunrise-sounds/
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
# ── Attempt overlay + audio (falls back to overlay-only if audio fails) ───────
audio_mixed=false
step3_ok=true
if [ -n "$audio_src" ]; then
echo "Step 3/3: overlay + audio → $final_video"
if ffmpeg -loglevel warning \
-i "$sped_video" \
-ss "$audio_offset" -t "$SUNRISE_TARGET_SECS" -i "$audio_src" \
-filter_complex "[0:v]${DT}[vout];[1:a]afade=t=in:st=0:d=0.5,afade=t=out:st=${fade_out}:d=0.5[aout]" \
-map "[vout]" -map "[aout]" \
-c:v libx264 -pix_fmt yuv420p -crf "$CRF_SUNRISE" \
-c:a aac -b:a 128k \
-y "$final_video"; then
audio_mixed=true
else
echo "Audio mix failed — retrying with overlay only"
"$SCRIPT_DIR/notify.sh" "WARNING: sunrise audio mix failed $current_date" \
"Audio could not be mixed — falling back to overlay-only" || true
[ "$audio_src" = "$cam_audio" ] && rm -f "$cam_audio"
fi
fi
if ! $audio_mixed; then
echo "Step 3/3: overlay (no audio) → $final_video"
ffmpeg -loglevel warning \
-i "$sped_video" \
-vf "${DT}" \
-c:v libx264 -pix_fmt yuv420p -crf "$CRF_SUNRISE" -an \
-y "$final_video" || step3_ok=false
fi
if $step3_ok; then
actual_dur=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$final_video")
echo "Done: $final_video (${actual_dur}s, sunrise at ${SR_TIME})"
rm -f "$sped_video"
[ -f "$cam_audio" ] && rm -f "$cam_audio"
"$SCRIPT_DIR/notify.sh" "Sunrise ready: $current_date" \
"$(basename "$final_video")${actual_dur}s, sunrise at ${SR_TIME}" || true
else
mv "$sped_video" "$final_video"
echo "Overlay failed — promoting speed-only video as upload target"
"$SCRIPT_DIR/notify.sh" "WARNING: sunrise overlay failed $current_date" \
"Overlay step failed — uploading speed-only video (no timestamp)" || true
fi