Files
sky-cam/daily_sunrise_video.sh
T
Claude 4ca1f579d2 Rename sunrise_video.10s.sh → daily_sunrise_video.sh
Name no longer implies a fixed duration. Updated all references:
install.sh, sky-cam.conf script map, systemd/sky-cam-sunrise.service.

https://claude.ai/code/session_01C4jbd3waXG3eKZYbGUjLUQ
2026-04-19 00:52:24 +00:00

153 lines
5.1 KiB
Bash

#!/bin/bash
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
source "$SCRIPT_DIR/sky-cam.conf"
# Get today's date in the format YYYY-MM-DD
current_date=$(date +%Y-%m-%d)
output_date=$(date +%Y-%m)
# Define the directory where images are stored (using today's date)
image_dir="$BASE_DIR/$CAM_NAME/$current_date"
# Set output directory
output_dir="$BASE_DIR/movies/$CAM_NAME/$output_date/sunrise-only"
# Make sure the output directory exists
mkdir -p "$output_dir"
# Get the sunrise time for today using the sunrise.py script
sunrise_time=$(python3 "$SCRIPT_DIR/sunrise.py")
# Check if the sunrise time was fetched successfully
if [[ -z "$sunrise_time" ]]; then
echo "Error: Failed to retrieve sunrise time."
exit 1
fi
# Output the sunrise time in UTC
echo "Sunrise time (UTC): $sunrise_time"
# Export timezone so date and subprocesses use the configured local time.
export TZ="$TIMEZONE"
# Convert sunrise time from UTC to local time.
sunrise_time_et=$(TZ="$TIMEZONE" date -d "$sunrise_time" +"%H-%M-%S")
# Output the converted sunrise time in Eastern Time
echo "Sunrise time (Eastern Time): $sunrise_time_et"
# Function to convert time to seconds since midnight
time_to_seconds() {
IFS='-' read -r h m s <<< "$1"
echo $((10#$h * 3600 + 10#$m * 60 + 10#$s))
}
# Convert sunrise time (in Eastern Time) to seconds since midnight
sunrise_seconds=$(time_to_seconds "$sunrise_time_et")
# Calculate the capture window around sunrise.
start_seconds=$((sunrise_seconds - SUNRISE_PRE_MIN * 60))
end_seconds=$((sunrise_seconds + SUNRISE_POST_MIN * 60))
# Output the start and end times in seconds for debugging
echo "Start seconds: $start_seconds"
echo "End seconds: $end_seconds"
echo "Sunrise seconds: $sunrise_seconds"
# Check if the image directory exists
if [ ! -d "$image_dir" ]; then
echo "Error: The directory for images does not exist: $image_dir"
exit 1
fi
# Create a temporary file to store the list of image files
temp_file=$(mktemp)
# Find, sort, and process images in the specified directory
# Sort files by filename, which will order them correctly by MM-DD-SS
find "$image_dir" -type f -name "*.jpg" | sort -n | while read -r image; do
# Extract the time portion of the filename (e.g., 09-16-00 from 09-16-00.jpg)
image_time=$(basename "$image" .jpg) # MM-DD-SS
# Convert image time to seconds since midnight
image_seconds=$(time_to_seconds "$image_time")
# Debug: Output the image time and its seconds since midnight
echo "Image: $image, Time: $image_time, Seconds: $image_seconds"
# Check if the image time falls within the specified range
if [[ $image_seconds -ge $start_seconds && $image_seconds -le $end_seconds ]]; then
echo "Image $image_time ($image_seconds) is within the range [$start_seconds, $end_seconds]"
# Add the image to the temporary file
echo "file '$image'" >> "$temp_file"
fi
done
# Check if any images were found in the time range
if [ ! -s "$temp_file" ]; then
echo "No images found within the specified time range."
exit 1
fi
# Calculate the number of images
num_images=$(wc -l < "$temp_file")
echo "Total images to be included in the video: $num_images"
# Output video filename for the first movie (no speed adjustments)
first_movie="$output_dir/$current_date-sunrise-day.mp4"
# Create the first movie at the default frame rate (no speed adjustments)
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 "$first_movie"
# Check the duration of the created video
video_duration=$(ffmpeg -i "$first_movie" 2>&1 | grep "Duration" | cut -d ' ' -f 4 | sed s/,//)
echo "Initial video duration: $video_duration"
# 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
target_duration=$SUNRISE_TARGET_SECS
speed_up_factor=$(echo "scale=3; $total_seconds / $target_duration" | bc)
echo "Calculated speed-up factor: $speed_up_factor"
# Output filename for the second movie (speed-adjusted)
final_video="$output_dir/$current_date-daily-sunrise.mp4"
# Apply the speed adjustment using the setpts filter
echo "Adjusting video speed to ${SUNRISE_TARGET_SECS}s..."
ffmpeg -i "$first_movie" -filter:v "setpts=PTS/$speed_up_factor" -y "$final_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 temporary files
rm "$temp_file"
# Run the sunrise_overlay.py script to add the sunrise overlay
python3 "$SCRIPT_DIR/sunrise_overlay.py" "$final_video"
# Run the sunrise2mm.py script to upload the video
python3 "$SCRIPT_DIR/sunrise2mm.py"
# Check if the Python script was successful
if [ $? -eq 0 ]; then
echo "Successfully uploaded the video to Mattermost."
else
echo "There was an issue uploading the video to Mattermost."
fi