diff --git a/4-seasons.sh b/4-seasons.sh old mode 100644 new mode 100755 index 3db8251..a58cff1 --- a/4-seasons.sh +++ b/4-seasons.sh @@ -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)" diff --git a/fullday-video.sh b/fullday-video.sh new file mode 100755 index 0000000..418a9c1 --- /dev/null +++ b/fullday-video.sh @@ -0,0 +1,82 @@ +#!/bin/bash +# fullday-video.sh — create an ~30-minute timelapse of yesterday's full day of +# images. Files older than 10 days are deleted automatically to save space. +# Run once per day (e.g. from cron at 02:00, after 4-seasons.sh). + +set -euo pipefail + +# ── Configuration ────────────────────────────────────────────────────────────── +base_dir="/home/motion/drives/local-2tb" +script_name=$(basename "$(dirname "$(realpath "$0")")" | cut -d'-' -f1) + +TARGET_DURATION=1800 # target video length in seconds (~30 min) +MIN_FRAME_DUR=0.016667 # max 60 fps +MAX_FRAME_DUR=10.0 # min 0.1 fps (10 s per frame) +RETENTION_DAYS=10 + +TARGET_W=1280 +TARGET_H=720 + +# ── Date / paths ────────────────────────────────────────────────────────────── +yesterday=$(date --date="yesterday" +%Y-%m-%d) +image_dir="$base_dir/$script_name/$yesterday" +output_dir="$base_dir/movies/$script_name/fullday" +mkdir -p "$output_dir" + +# ── Purge old full-day videos ───────────────────────────────────────────────── +echo "Removing full-day videos older than $RETENTION_DAYS days..." +find "$output_dir" -maxdepth 1 -name "*-fullday.mp4" -mtime "+$RETENTION_DAYS" -delete + +# ── Sanity checks ───────────────────────────────────────────────────────────── +if [ ! -d "$image_dir" ]; then + echo "WARNING: Image directory $image_dir not found — skipping." + exit 0 +fi + +num_images=$(find "$image_dir" -type f -name "*.jpg" | wc -l) +if [ "$num_images" -lt 1 ]; then + echo "WARNING: No images found in $image_dir — skipping." + exit 0 +fi +echo "Images: $num_images" + +# ── Calculate per-frame duration to hit TARGET_DURATION ─────────────────────── +frame_dur=$(echo "scale=6; $TARGET_DURATION / $num_images" | bc) + +# Clamp to [MIN_FRAME_DUR, MAX_FRAME_DUR] +if (( $(echo "$frame_dur < $MIN_FRAME_DUR" | bc -l) )); then frame_dur=$MIN_FRAME_DUR; fi +if (( $(echo "$frame_dur > $MAX_FRAME_DUR" | bc -l) )); then frame_dur=$MAX_FRAME_DUR; fi + +fps=$(echo "scale=4; 1 / $frame_dur" | bc) +approx_min=$(echo "scale=1; $num_images * $frame_dur / 60" | bc) +echo "Frame duration: ${frame_dur}s (fps=${fps}) ≈ ${approx_min} min" + +# ── Build concat list with per-frame duration ───────────────────────────────── +# The concat demuxer requires the last file to be repeated without a duration. +temp_list=$(mktemp --suffix=.txt) +trap 'rm -f "$temp_list"' EXIT + +last_img="" +find "$image_dir" -type f -name "*.jpg" | sort | while read -r img; do + echo "file '$img'" + echo "duration $frame_dur" + last_img="$img" +done > "$temp_list" + +# Append last image again (required by concat demuxer to set final frame pts) +last_img=$(find "$image_dir" -type f -name "*.jpg" | sort | tail -n 1) +echo "file '$last_img'" >> "$temp_list" + +# ── Encode ──────────────────────────────────────────────────────────────────── +output_file="$output_dir/${yesterday}-fullday.mp4" +echo "Creating full-day video: $output_file" + +ffmpeg -loglevel warning \ + -f concat -safe 0 -i "$temp_list" \ + -vf "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 28 \ + -an -y "$output_file" + +actual_dur=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$output_file") +echo "Done: $output_file (${actual_dur}s)" diff --git a/montage-mvt.sh b/montage-mvt.sh old mode 100644 new mode 100755 index 6d6acf3..2940657 --- a/montage-mvt.sh +++ b/montage-mvt.sh @@ -1,108 +1,171 @@ #!/bin/bash +# montage-mvt.sh — compile daily sunrise clips into a seasonal movement montage. +# Output: daily clips concatenated, speed-adjusted to match music duration, +# with 2-second video/audio fade in/out, followed by a 7-second attribution card. +# +# Music: "The Four Seasons" by Antonio Vivaldi +# Performed by John Harrison with the Wichita State University Chamber Players +# Source: Free Music Archive (freemusicarchive.org) +# License: CC BY-NC-SA 4.0 — verify at the URL above before redistribution. -# Set parameters dynamically based on the current date -current_date=$(date +%Y-%m-%d) -current_year=$(date +%Y) -day_of_year=$(date +%j) # The day of the year (e.g., 1-365) +set -euo pipefail -# Define season and movement parameters based on the current date -if [ $day_of_year -ge 265 ] && [ $day_of_year -le 355 ]; then - season="Autumn" - movement_num=3 - season_start_date=265 - season_end_date=355 - days_in_season=91 - day_of_season=$((day_of_year - season_start_date + 1)) # Calculate the day of the season -elif [ $day_of_year -ge 1 ] && [ $day_of_year -le 79 ]; then - season="Winter" - movement_num=1 - season_start_date=1 - season_end_date=79 - days_in_season=79 - day_of_season=$((day_of_year - season_start_date + 1)) -elif [ $day_of_year -ge 80 ] && [ $day_of_year -le 171 ]; then - season="Spring" - movement_num=2 - season_start_date=80 - season_end_date=171 - days_in_season=92 - day_of_season=$((day_of_year - season_start_date + 1)) -elif [ $day_of_year -ge 172 ] && [ $day_of_year -le 264 ]; then - season="Summer" - movement_num=4 - season_start_date=172 - season_end_date=264 - days_in_season=93 - day_of_season=$((day_of_year - season_start_date + 1)) +# ── Configuration ────────────────────────────────────────────────────────────── +base_dir="/home/motion/drives/local-2tb" +script_name=$(basename "$(dirname "$(realpath "$0")")" | cut -d'-' -f1) +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" + +TARGET_W=1280 +TARGET_H=720 +FADE_DUR=2.0 +ATTR_DUR=7 + +# ── Season / movement detection ──────────────────────────────────────────────── +day_of_year=$(date +%j | sed 's/^0*//') # strip leading zeros for arithmetic + +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=173; days_in_season=92 +elif [ "$day_of_year" -ge 265 ] && [ "$day_of_year" -le 355 ]; then + season="Autumn"; season_start=265; days_in_season=91 else - echo "Season not found for the current date" - exit 1 + season="Winter"; days_in_season=90 + # Winter straddles the year boundary (day 356–79) + if [ "$day_of_year" -ge 356 ]; then season_start=356; else season_start=0; fi fi -# Debugging info: Print dynamic parameters -echo "Season: $season" -echo "Movement: $movement_num" -echo "Day of Year: $day_of_year" - -# Set the directory for the videos dynamically based on the season and movement -video_base_dir="/home/motion/drives/local-2tb/movies/sunrise/$season/Mvt$movement_num" -echo "Looking for video files in: $video_base_dir" - -# Ensure the video directory exists -if [ ! -d "$video_base_dir" ]; then - echo "Video directory $video_base_dir not found" - exit 1 -fi - -# Find the music file for the current season and movement -music_file=$(find "/home/motion/drives/local-2tb/music/4 Seasons" -type f -iname "*$season Mvt $movement_num*" | head -n 1) - -# Ensure the music file exists -if [ ! -f "$music_file" ]; then - echo "Music file for $season Mvt $movement_num not found" - exit 1 -fi - -# 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" - -# Find all video files ending in "-final.mp4" using ls and grep -video_files=() -echo "Looking for video files in: $video_base_dir" -for video_file in $(ls "$video_base_dir" | grep -i "Mvt${movement_num}.*-final.mp4"); do - full_path="$video_base_dir/$video_file" - if [ -f "$full_path" ]; then - video_files+=("$full_path") - fi -done - -# Check if video files were found -if [ ${#video_files[@]} -eq 0 ]; then - echo "No video files found for $season Mvt$movement_num in $video_base_dir" - exit 1 +# 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 - echo "Found ${#video_files[@]} video files for $season Mvt$movement_num" + day_of_season=$(( day_of_year - season_start + 1 )) fi -# Prepare a temporary file for concatenation -temp_file=$(mktemp) -for video_file in "${video_files[@]}"; do - echo "file '$video_file'" >> "$temp_file" -done +# Each season split into three movements of ~30 days each +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 -# Set the output directory and file for the montage -output_dir="$video_base_dir/$current_year-$month" +echo "Season=$season Mvt=$movement_num DayOfSeason=$day_of_season DaysInMvt=$days_in_movement" + +# ── Locate input files ───────────────────────────────────────────────────────── +video_dir="$base_dir/movies/$script_name/$season/Mvt$movement_num" +output_dir="$video_dir/montage" mkdir -p "$output_dir" -output_file="$output_dir/$(date +"%Y-%m-%d")_Mvt$movement_num-Montage.mp4" -# Create the final montage by concatenating the video files with the music -echo "Creating montage for $season Mvt $movement_num..." +music_file=$(find "$music_base_dir" -type f -iname "*${season}*Mvt*${movement_num}*" | sort | head -n 1) +if [ -z "$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)" -# Use ffmpeg to concatenate the video files and overlay the music -ffmpeg -f concat -safe 0 -i "$temp_file" -i "$music_file" -c:v libx264 -pix_fmt yuv420p -c:a aac -strict experimental -shortest "$output_file" +# Collect sorted daily clips +mapfile -d '' daily_clips < <(find "$video_dir" -maxdepth 1 -name "*-final.mp4" -print0 | sort -z) +if [ "${#daily_clips[@]}" -eq 0 ]; then + echo "ERROR: No *-final.mp4 clips found in $video_dir" + exit 1 +fi +echo "Found ${#daily_clips[@]} daily clips" -# Clean up temporary file -rm "$temp_file" +# ── Temp-file tracking for cleanup ──────────────────────────────────────────── +TMPFILES=() +cleanup() { rm -f "${TMPFILES[@]}" 2>/dev/null || true; } +trap cleanup EXIT -echo "Montage created: $output_file" +concat_list=$(mktemp --suffix=.txt); TMPFILES+=("$concat_list") +printf "file '%s'\n" "${daily_clips[@]}" > "$concat_list" + +# ── Step 1: Concatenate + normalise to TARGET_W x TARGET_H ──────────────────── +temp_concat=$(mktemp --suffix=.mp4); TMPFILES+=("$temp_concat") +echo "Step 1/5: concatenating ${#daily_clips[@]} clips at ${TARGET_W}x${TARGET_H}..." +ffmpeg -loglevel warning \ + -f concat -safe 0 -i "$concat_list" \ + -vf "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 -an -y "$temp_concat" + +# ── Step 2: Speed-adjust so total video == music duration ───────────────────── +concat_dur=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$temp_concat") +speed_factor=$(echo "scale=6; $concat_dur / $music_duration" | bc) +echo "Step 2/5: speed factor $speed_factor (raw=${concat_dur}s target=${music_duration}s)" + +temp_sped=$(mktemp --suffix=.mp4); TMPFILES+=("$temp_sped") +ffmpeg -loglevel warning \ + -i "$temp_concat" \ + -vf "setpts=PTS/$speed_factor" \ + -c:v libx264 -pix_fmt yuv420p -crf 26 -an \ + -t "$music_duration" -y "$temp_sped" +rm "$temp_concat" + +# ── Step 3: Merge with music + video/audio fades ────────────────────────────── +fade_out_start=$(echo "scale=3; $music_duration - $FADE_DUR" | bc) +echo "Step 3/5: merging music + fade in/out (fade=${FADE_DUR}s)..." + +temp_main=$(mktemp --suffix=.mp4); TMPFILES+=("$temp_main") +ffmpeg -loglevel warning \ + -i "$temp_sped" -i "$music_file" \ + -filter_complex \ +"[0:v]fade=t=in:st=0:d=${FADE_DUR},fade=t=out:st=${fade_out_start}:d=${FADE_DUR}[vout];\ +[1:a]afade=t=in:st=0:d=${FADE_DUR},afade=t=out:st=${fade_out_start}:d=${FADE_DUR}[aout]" \ + -map "[vout]" -map "[aout]" \ + -c:v libx264 -pix_fmt yuv420p -c:a aac -b:a 192k \ + -t "$music_duration" -y "$temp_main" +rm "$temp_sped" + +# ── Step 4: Create 7-second attribution card ────────────────────────────────── +echo "Step 4/5: building attribution card..." + +# Locate a usable font +FONT="" +FONT_BOLD="" +if command -v fc-match &>/dev/null; then + FONT=$(fc-match "DejaVu Sans:style=Regular" --format="%{file}" 2>/dev/null || true) + FONT_BOLD=$(fc-match "DejaVu Sans:style=Bold" --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 +[ -z "$FONT_BOLD" ] && FONT_BOLD="$FONT" + +# Build drawtext chain (colons in text values escaped as \: → \\: in bash string) +DT="drawtext=fontfile='${FONT_BOLD}':text='The Four Seasons - Antonio Vivaldi':fontcolor=white:fontsize=38:x=(w-text_w)/2:y=h*0.18" +DT="${DT},drawtext=fontfile='${FONT}':text='Performed by John Harrison':fontcolor=0xDDDDDD:fontsize=28:x=(w-text_w)/2:y=h*0.34" +DT="${DT},drawtext=fontfile='${FONT}':text='with the Wichita State University Chamber Players':fontcolor=0xDDDDDD:fontsize=22:x=(w-text_w)/2:y=h*0.44" +DT="${DT},drawtext=fontfile='${FONT}':text='Source\\: Free Music Archive':fontcolor=white:fontsize=21:x=(w-text_w)/2:y=h*0.59" +DT="${DT},drawtext=fontfile='${FONT}':text='freemusicarchive.org':fontcolor=0xFFFF88:fontsize=21:x=(w-text_w)/2:y=h*0.68" +DT="${DT},drawtext=fontfile='${FONT}':text='License\\: CC BY-NC-SA 4.0':fontcolor=0xCCCCCC:fontsize=19:x=(w-text_w)/2:y=h*0.78" +DT="${DT},fade=t=in:st=0:d=1,fade=t=out:st=$((ATTR_DUR - 1)):d=1" + +temp_attr=$(mktemp --suffix=.mp4); TMPFILES+=("$temp_attr") +ffmpeg -loglevel warning \ + -f lavfi -i "color=c=black:s=${TARGET_W}x${TARGET_H}:r=25:d=${ATTR_DUR}" \ + -f lavfi -i "anullsrc=r=44100:cl=stereo" \ + -vf "$DT" \ + -c:v libx264 -pix_fmt yuv420p -c:a aac -b:a 192k \ + -t "$ATTR_DUR" -y "$temp_attr" + +# ── Step 5: Concatenate montage + attribution ───────────────────────────────── +echo "Step 5/5: appending attribution and writing final file..." +final_list=$(mktemp --suffix=.txt); TMPFILES+=("$final_list") +printf "file '%s'\n" "$temp_main" "$temp_attr" > "$final_list" + +output_file="$output_dir/$(date +%Y-%m-%d)_${season}_Mvt${movement_num}-Montage.mp4" +ffmpeg -loglevel warning \ + -f concat -safe 0 -i "$final_list" \ + -c copy -y "$output_file" + +echo "Done: $output_file"