Last active
May 25, 2024 13:27
-
-
Save oliverste/76ef29657511c034b2bb1dbb742815b6 to your computer and use it in GitHub Desktop.
This script processes files in a specified directory and updates their modification and creation dates based on available EXIF metadata (DateTimeOriginal, CreateDate, ModifyDate). If the original dates are missing or invalid, it attempts to use alternative timestamps. The script also updates corresponding .MP4 files if they exist. I used this sc…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Set the directory to process (update this path to the desired directory) | |
directory="/path/to/your/photos" | |
# Toggle to ignore the incorrect modification date check (set to "true" or "false") | |
ignore_incorrect_mod_date=false | |
# Echo the directory being processed | |
echo "Processing directory: $directory" | |
# Check if the directory exists | |
if [ ! -d "$directory" ]; then | |
echo "Directory does not exist: $directory" | |
exit 1 | |
fi | |
# Define the incorrect modification date pattern to search for | |
incorrect_mod_date="1970-01-01 01:00:00.000000000 +0100" | |
# Function to update dates | |
update_dates() { | |
local file="$1" | |
echo "Updating file: $file" | |
# Check for DateTimeOriginal, CreateDate, and ModifyDate | |
datetime_original=$(exiftool -DateTimeOriginal -s3 "$file") | |
create_date=$(exiftool -CreateDate -s3 "$file") | |
modify_date=$(exiftool -ModifyDate -s3 "$file") | |
# Determine which date to use | |
if [ -n "$datetime_original" ]; then | |
exiftool "-DateTimeOriginal>FileModifyDate" "-DateTimeOriginal>FileCreateDate" "$file" | |
mod_date="$datetime_original" | |
echo "Set dates from DateTimeOriginal: $mod_date" | |
elif [ -n "$create_date" ]; then | |
exiftool "-CreateDate>FileModifyDate" "-CreateDate>FileCreateDate" "$file" | |
mod_date="$create_date" | |
echo "Set dates from CreateDate: $mod_date" | |
elif [ -n "$modify_date" ]; then | |
exiftool "-ModifyDate>FileModifyDate" "-ModifyDate>FileCreateDate" "$file" | |
mod_date="$modify_date" | |
echo "Set dates from ModifyDate: $mod_date" | |
else | |
echo "No suitable date tag found for $file" | |
return | |
fi | |
# Convert the date to the format required by touch (YYYY-MM-DD HH:MM:SS) | |
mod_date_formatted=$(echo "$mod_date" | sed 's/:/-/g' | sed 's/ /-/') | |
# Check for corresponding .MP4 file (if applicable) | |
mp4_file="${file%.*}.MP4" | |
if [ -f "$mp4_file" ]; then | |
echo "Updating corresponding MP4 file: $mp4_file" | |
touch -d "$mod_date_formatted" "$mp4_file" | |
exiftool "-FileModifyDate=$mod_date_formatted" "-FileCreateDate=$mod_date_formatted" "$mp4_file" | |
echo "Set MP4 dates to: $mod_date_formatted" | |
fi | |
} | |
# Find all files in the specified directory and its subdirectories | |
find "$directory" -type f | while read -r file; do | |
if $ignore_incorrect_mod_date || stat "$file" | grep -q "Modify: $incorrect_mod_date"; then | |
update_dates "$file" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment