From: Siraaj Khandkar Date: Wed, 26 Jan 2022 14:44:20 +0000 (-0500) Subject: Prototype photo organizer X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=7605b8af0417681fdfb1525cfe1ceafcfccd8788;p=khome.git Prototype photo organizer --- diff --git a/home/bin/photo-org b/home/bin/photo-org new file mode 100755 index 0000000..93dbc38 --- /dev/null +++ b/home/bin/photo-org @@ -0,0 +1,43 @@ +#! /bin/bash + +input_dir="$1" +output_dir="$2" + +case "$output_dir" in + '') output_dir="$input_dir/auto-organized" +esac + +find "$input_dir" -type f -iregex '^.+\.jp[e]*g$' | while read -r path; do + # TODO Use "exiftool" instead of "file". Unclear which timestamp field to use. + file_data=$(file "$path") + if echo "$file_data" | grep 'image data' > /dev/null; then + read -r year0 month0 day0 hour0 minute0 second0 < <( + echo "$file_data" \ + | grep -o 'Exif Standard: \[.*\]' \ + | grep -oE 'datetime=[0-9]{4}:[0-9]{2}:[0-9]{2} +[0-9]{2}:[0-9]{2}:[0-9]{2}' \ + | sed 's/^datetime=//' \ + | sed 's/:/ /g' + ) + if ! [[ "$year0" = '' && "$month0" = '' && "$day0" = '' && \ + "$year0" -eq 0 && "$month0" -eq 0 && "$day0" -eq 0 ]] + then + # Force base 10, since leading zeros may cause an assumption of base 8: + year=$(( 10#${year0} )) + month=$(( 10#${month0} )) + day=$(( 10#${day0} )) + hour=$(( 10#${hour0} )) + minute=$((10#${minute0})) + second=$((10#${second0})) + + digest=$(sha256sum "$path" | awk '{print $1}') + suffix=$(echo "$path" | awk -F . '{print tolower($NF)}') + new_dir="${output_dir}/${year}/${month}/${day}" + new_name="${year}-${month}-${day}--${hour}-${minute}-${second}--${digest}.${suffix}" + new_path="$new_dir/$new_name" + + printf 'old: %s\n' "$path" + printf 'new: %s\n' "$new_path" + printf '\n' + fi + fi +done