myshellconfig/bin/exif-fixdatetime

121 lines
4.9 KiB
Text
Raw Normal View History

2020-11-10 17:26:25 +01:00
#!/bin/bash
2020-11-13 00:21:47 +01:00
[ -z "${move+x}" ] && move=false
2020-11-13 22:05:51 +01:00
[ -z "${overwrite+x}" ] && overwrite=false
2020-11-13 00:21:47 +01:00
2020-11-12 18:33:49 +01:00
export FILELOGLEVEL=WARN
export SCRIPT_LOG=~/logs/exif.log
2020-11-10 17:26:25 +01:00
parsedatetime() {
filename="$(basename "${@}")"
2020-11-13 22:05:51 +01:00
# 2015-09-11_17:41:53-1.jpg
if [[ $filename =~ ^([0-9]{4})(\.|-)([0-9]{2})(\.|-)([0-9]{2})( |_)([0-9]{2})(\.|-|:)([0-9]{2})(\.|-|:)([0-9]{2})(\.|-).*$ ]]; then
2020-11-10 17:26:25 +01:00
result="$(printf "%04d:%02d:%02d %02d:%02d:%02d" "${BASH_REMATCH[1]#0}" "${BASH_REMATCH[3]#0}" "${BASH_REMATCH[5]#0}" "${BASH_REMATCH[7]#0}" "${BASH_REMATCH[9]#0}" "${BASH_REMATCH[11]#0}")"
fi
2020-11-13 22:05:51 +01:00
if [ -z "${result}" ]; then
if [[ $filename =~ ^([0-9]{4})([0-9]{2})([0-9]{2})(_)([0-9]{2})([0-9]{2})([0-9]{2}).*$ ]]; then
result="$(printf "%04d:%02d:%02d %02d:%02d:%02d" "${BASH_REMATCH[1]#0}" "${BASH_REMATCH[2]#0}" "${BASH_REMATCH[3]#0}" "${BASH_REMATCH[5]#0}" "${BASH_REMATCH[6]#0}" "${BASH_REMATCH[7]#0}")"
fi
2020-11-10 17:26:25 +01:00
fi
2020-11-13 22:05:51 +01:00
if [ -z "${result}" ]; then
# Whatsapp-Bilder
if [[ $filename =~ ^(IMG-)([0-9]{4})([0-9]{2})([0-9]{2})(-WA)([0-9]{2})([0-9]{2}).*$ ]]; then
result="$(printf "%04d:%02d:%02d %02d:%02d:%02d" "${BASH_REMATCH[2]#0}" "${BASH_REMATCH[3]#0}" "${BASH_REMATCH[4]#0}" "${BASH_REMATCH[6]#0}" "${BASH_REMATCH[7]#0}" "00")"
fi
2020-11-10 17:26:25 +01:00
fi
2020-11-13 22:05:51 +01:00
if [ -z "${result}" ]; then
# eYe20130323123316.jpg
if [[ $filename =~ ^(eYe)([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2}).*$ ]]; then
result="$(printf "%04d:%02d:%02d %02d:%02d:%02d" "${BASH_REMATCH[2]#0}" "${BASH_REMATCH[3]#0}" "${BASH_REMATCH[4]#0}" "${BASH_REMATCH[5]#0}" "${BASH_REMATCH[6]#0}" "${BASH_REMATCH[7]#0}")"
fi
fi
if [ -z "${result}" ]; then
# ./2016-01-22/IMG_4225.jpg
if [[ "$(dirname ${@})" =~ ^.*/([0-9]{4})(\.|-)([0-9]{2})(\.|-)([0-9]{2})$ ]]; then
result="$(printf "%04d:%02d:%02d %02d:%02d:%02d" "${BASH_REMATCH[1]#0}" "${BASH_REMATCH[3]#0}" "${BASH_REMATCH[5]#0}" "00" "00" "00")"
fi
2020-11-12 18:33:49 +01:00
fi
2020-11-10 17:26:25 +01:00
echo "$result"
return 0
}
fix_datetime () {
file="${@}"
loginfo "file: $file "
newDateTimeOriginal="$(parsedatetime "$file")"
loginfo "newDateTimeOriginal: $newDateTimeOriginal"
2020-11-12 18:33:49 +01:00
mime="$(file --mime-type "$file" | awk '{print $NF}')"
2020-11-10 17:26:25 +01:00
2020-11-10 17:30:13 +01:00
if [ -n "$newDateTimeOriginal" ]; then
2020-11-12 18:33:49 +01:00
case $mime in
image/png)
exiftool "-creationtime=$newDateTimeOriginal" -if '(not $creationtime)' "$file"
exiftool "-DateTimeOriginal=$newDateTimeOriginal" -if '(not $datetimeoriginal)' "$file"
exiftool '-createdate<datetimeoriginal' -if '(not $createdate and $datetimeoriginal)' "$file"
2020-11-13 22:05:51 +01:00
DEST="Bilder/Tina_und_Jakob"
;;
image/jpeg)
if $overwrite; then
exiftool "-DateTimeOriginal=$newDateTimeOriginal" "$file"
exiftool '-createdate<datetimeoriginal' -if '($datetimeoriginal)' "$file"
else
exiftool "-DateTimeOriginal=$newDateTimeOriginal" -if '(not $datetimeoriginal)' "$file"
exiftool '-createdate<datetimeoriginal' -if '(not $createdate and $datetimeoriginal)' "$file"
fi
DEST="Bilder/Tina_und_Jakob"
;;
video/mp4|video/m4a)
if $overwrite; then
exiftool "-DateTimeOriginal=$newDateTimeOriginal" "$file"
exiftool '-createdate<datetimeoriginal' -if '($datetimeoriginal)' "$file"
else
exiftool "-DateTimeOriginal=$newDateTimeOriginal" -if '(not $datetimeoriginal)' "$file"
exiftool '-createdate<datetimeoriginal' -if '(not $createdate and $datetimeoriginal)' "$file"
fi
DEST="Videos/Tina_und_Jakob"
2020-11-12 18:33:49 +01:00
;;
*)
logwarn "unsupportet mimetype $mime for $file"
;;
esac
2020-11-10 17:26:25 +01:00
fi
2020-11-13 14:55:07 +01:00
2020-11-10 17:30:13 +01:00
loginfo "$(exiftool -filename -createdate -datetimeoriginal "$file")"
2020-11-13 00:21:47 +01:00
2020-11-13 22:05:51 +01:00
2020-11-13 14:55:07 +01:00
case $move in
true)
2020-11-13 22:05:51 +01:00
echo move
loginfo "$(exiftool '-Directory<DateTimeOriginal' -d /srv/nfs/data/userdata/SHARED/${DEST}/%Y/%Y_%m -if '($datetimeoriginal)' "$file" 2>&1 |tee -a dest_exists)"
2020-11-13 14:55:07 +01:00
;;
copy)
2020-11-13 22:05:51 +01:00
echo copy
loginfo "$(exiftool -o . '-Directory<DateTimeOriginal' -d /srv/nfs/data/userdata/SHARED/${DEST}/%Y/%Y_%m -filename -createdate -datetimeoriginal -if '($datetimeoriginal)' "$file")"
2020-11-13 14:55:07 +01:00
logwarn "$(readlink -f "$file"): $(exiftool -filename -if '(not ($datetimeoriginal or $createdate))' "$file")"
;;
*)
logwarn "value for move is neither »true« nor »copy«"
esac
2020-11-10 17:30:13 +01:00
loginfo "----------------"
loginfo ""
2020-11-10 17:26:25 +01:00
}
if [ $# -gt 0 ]; then
loginfo "Progress only one file from cmdline ${@}"
2020-11-12 18:33:49 +01:00
fix_datetime "${@}"
2020-11-10 17:26:25 +01:00
else
loginfo "Progress all *.jpg files in pwd $(pwd)"
2020-11-12 18:33:49 +01:00
find -iname "*.jp*g"|while read i; do
2020-11-10 17:26:25 +01:00
fix_datetime "$i"
done
fi