62 lines
1.5 KiB
Bash
62 lines
1.5 KiB
Bash
#!/usr/bin/env bash
|
|
# Renames images and videos in ./ based on metadata date
|
|
# Requires: exiftool
|
|
# Format: YYYY-MM-DD_HH-MM-SS.ext (appends _N if duplicate)
|
|
|
|
set -euo pipefail
|
|
|
|
TARGET="${1:-.}"
|
|
|
|
if ! command -v exiftool &>/dev/null; then
|
|
echo "Error: exiftool not found. Install it with: sudo apt install libimage-exiftool-perl" >&2
|
|
exit 1
|
|
fi
|
|
|
|
shopt -s nullglob nocaseglob
|
|
files=("$TARGET"/*.{jpg,jpeg,png,tiff,tif,heic,webp,mp4,mov,m4v,avi,mkv,webm})
|
|
shopt -u nullglob nocaseglob
|
|
|
|
if [[ ${#files[@]} -eq 0 ]]; then
|
|
echo "No image files found in: $TARGET"
|
|
exit 0
|
|
fi
|
|
|
|
for file in "${files[@]}"; do
|
|
[[ -f "$file" ]] || continue
|
|
|
|
dir=$(dirname "$file")
|
|
ext="${file##*.}"
|
|
ext_lower="${ext,,}"
|
|
|
|
# Try date tags in priority order
|
|
date_str=$(exiftool -d "%Y-%m-%d_%H-%M-%S" \
|
|
-DateTimeOriginal \
|
|
-CreateDate \
|
|
-ModifyDate \
|
|
-FileModifyDate \
|
|
-s3 "$file" 2>/dev/null | head -1)
|
|
|
|
if [[ -z "$date_str" || "$date_str" == *"0000"* ]]; then
|
|
echo "SKIP (no date): $file"
|
|
continue
|
|
fi
|
|
|
|
new_base="$dir/${date_str}.${ext_lower}"
|
|
|
|
# Handle duplicates
|
|
if [[ "$file" == "$new_base" ]]; then
|
|
echo "OK (unchanged): $file"
|
|
continue
|
|
fi
|
|
|
|
counter=1
|
|
final="$new_base"
|
|
while [[ -e "$final" ]]; do
|
|
final="$dir/${date_str}_${counter}.${ext_lower}"
|
|
((counter++))
|
|
done
|
|
|
|
mv -- "$file" "$final"
|
|
echo "RENAMED: $(basename "$file") -> $(basename "$final")"
|
|
done
|