#!/bin/bash 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 base_dir="/home/motion/drives/local-2tb" music_base_dir="/home/motion/drives/local-2tb/music/4 Seasons" # 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 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 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 else season="Winter" season_start_date=356 season_end_date=79 days_in_season=90 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 else day_of_season=$(($day_of_year - $season_start_date + 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 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) # Check if the music file exists if [ ! -f "$music_file" ]; then echo "Music file for $season, Movement $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" # Printing the music duration for verification # 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" # 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 exit 0 fi # Get today's date in yyyy-mm format for output directory output_date=$(date +"%Y-%m") # yyyy-mm format # Dynamically generate the output directory based on season and movement output_dir="$base_dir/movies/$first_word_of_script_folder/$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) # 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 # 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" # 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) 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" # 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" # 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 # 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" # 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" # 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"