Add seasonal montage with music/fades/attribution and full-day timelapse

4-seasons.sh:
- Fix per-video target duration: divide by days_in_movement (~30) not
  days_in_season (~90), so concatenated clips sum to the movement's
  music duration
- Force consistent 1280x720 output (scale+pad) to prevent resolution
  mismatches when montage-mvt.sh concatenates across days
- Use ffprobe (not ffmpeg stderr parsing) for reliable duration reading
- Add music-dir fallback to repo-local music/ folder
- Remove the 2× music-doubling workaround (root cause fixed above)

montage-mvt.sh — complete rewrite:
- Proper season/movement detection matching 4-seasons.sh logic
- Step 1: concat all *-final.mp4 clips, normalise to 1280x720
- Step 2: speed-adjust concatenated video to exactly match music duration
- Step 3: merge with Vivaldi movement audio + 2 s video/audio fade in/out
- Step 4: generate 7-second black attribution card (drawtext) crediting
  John Harrison / Wichita State University Chamber Players / FMA /
  CC BY-NC-SA 4.0
- Step 5: concat montage + attribution into final file
- Temp files cleaned up via EXIT trap

fullday-video.sh — new script:
- Full-day timelapse from yesterday's images targeting ~30 minutes
- Per-frame duration = 1800 / num_images (clamped 0.017–10 s/frame)
- 1280x720 output, video-only (no audio)
- Auto-deletes fullday videos older than 10 days

https://claude.ai/code/session_01C4jbd3waXG3eKZYbGUjLUQ
This commit is contained in:
Claude
2026-04-18 03:16:27 +00:00
parent 37fb9a5f30
commit 009f6b4b64
3 changed files with 322 additions and 236 deletions
Regular → Executable
+84 -143
View File
@@ -1,182 +1,123 @@
#!/bin/bash
# 4-seasons.sh — create one daily clip from yesterday's images, sized to its
# share of the corresponding Vivaldi movement's music duration.
# Run once per day (e.g. from cron at 01:00).
set -euo pipefail
set -x
# Get yesterday's date
yesterday=$(date --date="yesterday" +%Y-%m-%d)
current_year=$(date +%Y)
day_of_year=$(date --date="$yesterday" +%j) # Day of the year (1 to 366)
# Define base directory and music directory
# ── Configuration ──────────────────────────────────────────────────────────────
base_dir="/home/motion/drives/local-2tb"
music_base_dir="/home/motion/drives/local-2tb/music/4 Seasons"
music_base_dir="$base_dir/music/4 Seasons"
# Fallback: use music/ directory next to this script (repo copy)
[ ! -d "$music_base_dir" ] && music_base_dir="$(dirname "$(realpath "$0")")/music"
# Determine the season based on the day of the year
if [ "$day_of_year" -ge 80 ] && [ "$day_of_year" -le 172 ]; then
season="Spring"
season_start_date=80
season_end_date=172
days_in_season=92
TARGET_W=1280
TARGET_H=720
# ── Date setup ────────────────────────────────────────────────────────────────
yesterday=$(date --date="yesterday" +%Y-%m-%d)
day_of_year=$(date --date="$yesterday" +%j | sed 's/^0*//') # strip leading zeros
# ── Season / movement detection ────────────────────────────────────────────────
if [ "$day_of_year" -ge 80 ] && [ "$day_of_year" -le 172 ]; then
season="Spring"; season_start=80; days_in_season=93
elif [ "$day_of_year" -ge 173 ] && [ "$day_of_year" -le 264 ]; then
season="Summer"
season_start_date=173
season_end_date=264
days_in_season=92
season="Summer"; season_start=173; days_in_season=92
elif [ "$day_of_year" -ge 265 ] && [ "$day_of_year" -le 355 ]; then
season="Autumn"
season_start_date=265
season_end_date=355
days_in_season=91
season="Autumn"; season_start=265; days_in_season=91
else
season="Winter"
season_start_date=356
season_end_date=79
days_in_season=90
season="Winter"; days_in_season=90
if [ "$day_of_year" -ge 356 ]; then season_start=356; else season_start=0; fi
fi
# Calculate the day of the season (from 1 to days_in_season)
if [ "$season" == "Winter" ]; then
if [ "$day_of_year" -ge 355 ]; then
# Winter season starts at day 1 on Dec 21st
day_of_season=$(($day_of_year - 354)) # Day 1 of Winter starts on Dec 21st
else
# Before Dec 21st, Winter starts on Dec 21st in the previous year
day_of_season=$((365 - 354 + $day_of_year)) # Adjust for the 365th day (Dec 20th) in the previous year
fi
# Day within the season (1-based)
if [ "$season" = "Winter" ] && [ "$day_of_year" -lt 80 ]; then
day_of_season=$(( day_of_year + 365 - 355 ))
elif [ "$season" = "Winter" ]; then
day_of_season=$(( day_of_year - 355 ))
else
day_of_season=$(($day_of_year - $season_start_date + 1))
day_of_season=$(( day_of_year - season_start + 1 ))
fi
# Determine the movement based on the day of the season
if [ "$season" == "Winter" ]; then
# Winter typically has three movements, so we calculate the movement number
if [ "$day_of_season" -le 30 ]; then
movement_num=1
elif [ "$day_of_season" -le 60 ]; then
movement_num=2
else
movement_num=3
fi
else
# Other seasons follow the same calculation for movement
if [ "$day_of_season" -le 30 ]; then
movement_num=1
elif [ "$day_of_season" -le 60 ]; then
movement_num=2
else
movement_num=3
fi
# Each season: three movements of ~30 days; Mvt 3 takes the remainder
if [ "$day_of_season" -le 30 ]; then movement_num=1; days_in_movement=30
elif [ "$day_of_season" -le 60 ]; then movement_num=2; days_in_movement=30
else movement_num=3; days_in_movement=$(( days_in_season - 60 ))
fi
# Determine the music file based on the season and movement
music_file=$(find "$music_base_dir" -type f -iname "*$season Mvt $movement_num*" | head -n 1)
echo "Date=$yesterday Season=$season Mvt=$movement_num DayOfSeason=$day_of_season DaysInMvt=$days_in_movement"
# Check if the music file exists
if [ ! -f "$music_file" ]; then
echo "Music file for $season, Movement $movement_num not found!"
# ── Locate music file ─────────────────────────────────────────────────────────
music_file=$(find "$music_base_dir" -type f -iname "*${season}*Mvt*${movement_num}*" | sort | head -n 1)
if [ -z "$music_file" ] || [ ! -f "$music_file" ]; then
echo "ERROR: Music file not found for $season Mvt $movement_num in $music_base_dir"
exit 1
fi
music_duration=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$music_file")
echo "Music: $(basename "$music_file") ($music_duration s)"
# Get the duration of the music file in seconds
music_duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$music_file")
echo "Music duration: $music_duration seconds" # Printing the music duration for verification
# Each daily clip should cover music_duration / days_in_movement seconds so that
# the full movement montage sums to exactly the music duration.
target_duration_per_video=$(echo "scale=6; $music_duration / $days_in_movement" | bc)
echo "Target duration per daily clip: $target_duration_per_video s"
# Define the directory where images are stored (using yesterday's date)
first_word_of_script_folder=$(basename "$(dirname "$(realpath "$0")")" | cut -d'-' -f1)
image_dir="$base_dir/$first_word_of_script_folder/$yesterday"
# ── Locate images ─────────────────────────────────────────────────────────────
script_name=$(basename "$(dirname "$(realpath "$0")")" | cut -d'-' -f1)
image_dir="$base_dir/$script_name/$yesterday"
# Check if the image directory exists
if [ ! -d "$image_dir" ]; then
echo "Error: Image directory $image_dir not found for $yesterday!"
echo "Skipping this day's video generation."
# Log the missing day
echo "No images for $yesterday" >> /path/to/missing_days.log
# Skip to the next day
echo "WARNING: Image directory $image_dir not found for $yesterday — skipping."
exit 0
fi
# Get today's date in yyyy-mm format for output directory
output_date=$(date +"%Y-%m") # yyyy-mm format
total_images=$(find "$image_dir" -type f -name "*.jpg" | wc -l)
if [ "$total_images" -lt 1 ]; then
echo "WARNING: No images in $image_dir for $yesterday — skipping."
exit 0
fi
# Dynamically generate the output directory based on season and movement
output_dir="$base_dir/movies/$first_word_of_script_folder/$season/Mvt$movement_num"
# ── Prepare output directory ──────────────────────────────────────────────────
output_dir="$base_dir/movies/$script_name/$season/Mvt$movement_num"
mkdir -p "$output_dir"
# Count the total number of images (JPEG files)
total_images=$(find "$image_dir" -type f -name "*.jpg" | wc -l)
# ── Build sorted image list ────────────────────────────────────────────────────
temp_file=$(mktemp --suffix=.txt)
trap 'rm -f "$temp_file" "$temp_video"' EXIT
# If there are no images, log it and move to the next day
if [ "$total_images" -lt 1 ]; then
echo "Error: No images found in $image_dir for $yesterday."
echo "Skipping this day's video generation."
# Log the missing day
echo "No images for $yesterday" >> /path/to/missing_days.log
# Skip to the next day
exit 0
fi
find "$image_dir" -type f -name "*.jpg" | sort | while read -r img; do
echo "file '$img'"
done > "$temp_file"
# Create a temporary text file with the sorted list of image paths
temp_file=$(mktemp)
find "$image_dir" -type f -name "*.jpg" | sort -n | while read image; do
echo "file '$image'" >> "$temp_file"
done
# Calculate the number of images
num_images=$(wc -l < "$temp_file")
echo "Total images found for the video: $num_images"
echo "Images: $num_images"
# Calculate the target total duration (music duration + 3 seconds for transitions)
target_total_duration=$(echo "$music_duration + 3" | bc)
echo "Target total video duration (music + 3 seconds): $target_total_duration seconds"
# Calculate the target duration per video (based on number of days in the season)
target_duration_per_video=$(echo "$target_total_duration / $days_in_season" | bc -l)
echo "Each day's target video length: $target_duration_per_video seconds"
# Check if the daily video length is too short (less than 1.67 seconds)
if (( $(echo "$target_duration_per_video < 1.67" | bc -l) )); then
echo "Daily video length is less than 1.67 seconds. Doubling the music duration."
music_duration=$(echo "$music_duration * 2" | bc)
target_total_duration=$(echo "$music_duration + 3" | bc)
target_duration_per_video=$(echo "$target_total_duration / $days_in_season" | bc -l)
echo "New target total video duration (after doubling music): $target_total_duration seconds"
echo "New target duration per video: $target_duration_per_video seconds"
fi
# First, create the video using default frame rate (no speed-up or slow-down yet)
# ── Step 1: Build raw video at default frame rate ─────────────────────────────
temp_video="$output_dir/${yesterday}_Mvt${movement_num}-temp.mp4"
echo "Creating video at default frame rate..."
ffmpeg -f concat -safe 0 -i "$temp_file" -c:v libx264 -pix_fmt yuv420p -crf 28 -vsync 2 -y "$temp_video"
echo "Creating raw video..."
ffmpeg -loglevel warning \
-f concat -safe 0 -i "$temp_file" \
-c:v libx264 -pix_fmt yuv420p -crf 28 -vsync 2 -an \
-y "$temp_video"
# Check the duration of the created video
video_duration=$(ffmpeg -i "$temp_video" 2>&1 | grep "Duration" | cut -d ' ' -f 4 | sed s/,//)
echo "Initial video duration: $video_duration"
raw_duration=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$temp_video")
echo "Raw video duration: $raw_duration s"
# Get the video duration in seconds (from HH:MM:SS format)
IFS=':' read -r hours minutes seconds <<< $(echo $video_duration | cut -d '.' -f1) # Extract time
total_seconds=$(($hours * 3600 + $minutes * 60 + $seconds)) # Convert to total seconds
# ── Step 2: Speed-adjust to target duration + normalise resolution ─────────────
speed_factor=$(echo "scale=6; $raw_duration / $target_duration_per_video" | bc)
echo "Speed factor: $speed_factor"
# Calculate the speed-up factor to make the video fit into the target duration
speed_up_factor=$(echo "scale=3; $total_seconds / $target_duration_per_video" | bc)
echo "Calculated speed-up factor: $speed_up_factor"
final_file="$output_dir/${yesterday}_Mvt${movement_num}-Day${day_of_season}of${days_in_movement}-final.mp4"
ffmpeg -loglevel warning \
-i "$temp_video" \
-vf "setpts=PTS/$speed_factor,\
scale=${TARGET_W}:${TARGET_H}:force_original_aspect_ratio=decrease,\
pad=${TARGET_W}:${TARGET_H}:(ow-iw)/2:(oh-ih)/2,setsar=1" \
-c:v libx264 -pix_fmt yuv420p -crf 26 \
-t "$target_duration_per_video" \
-y "$final_file"
# Adjust the playback speed using the setpts filter to make the video fit into the target duration
final_file_name="${yesterday}_Mvt${movement_num}-Day${day_of_season}of${days_in_season}-final.mp4"
final_video="$output_dir/$final_file_name"
ffmpeg -i "$temp_video" -filter:v "setpts=PTS/$speed_up_factor" -y "$final_video"
rm "$temp_video"
# Check the duration of the adjusted video
adjusted_duration=$(ffmpeg -i "$final_video" 2>&1 | grep "Duration" | cut -d ' ' -f 4 | sed s/,//)
echo "Adjusted video duration: $adjusted_duration"
# Check for ffmpeg success
if [ $? -ne 0 ]; then
echo "Error: ffmpeg failed to create the final video."
exit 1
fi
# Inform user the video has been created
echo "Video created at $final_video"
# Clean up the temporary file
rm "$temp_file"
adjusted_duration=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$final_file")
echo "Daily clip created: $final_file ($adjusted_duration s)"