myshellconfig/bin/exif-fixdatetime

191 lines
7.1 KiB
Bash
Executable file

#!/bin/bash
export SCRIPT_LOG=./$(basename $0).log
[ -z "${move+x}" ] && move=false
[ -z "${forcedate+x}" ] && forcedate=false
[ -z "${setdate+x}" ] && setdate=true
[ -z "${dest+x}" ] && dest=/srv/nfs/data/userdata/SHARED && dest=${dest%/}
[ -z "${mimedestpicdefault+x}" ] && mimedestpicdefault="Bilder"
[ -z "${mimedestviddefault+x}" ] && mimedestviddefault="Videos"
[ -z "${subdestdefault+x}" ] && subdestdefault="Tina_und_Jakob"
[ -z "${mimedestpic+x}" ] && mimedestpic="${mimedestpicdefault}"
[ -z "${mimedestvid+x}" ] && mimedestvid="${mimedestviddefault}"
[ -z "${subdestpic+x}" ] && subdestpic="${subdestdefault}"
[ -z "${subdestvid+x}" ] && subdestvid="${subdestdefault}"
help () {
cat << EOF
Usage: $(basename $0) [move=[true|copy]] [setdate=[true|false]] [forcedate=[true|false]] [<file>]
Defaults:
# commands
move=$move [false|true|copy] false: file is not moved to destination; true: file is moved; copy: copy file to destination
forcedate=$forcedate [fase|true] false: datetimeoriginal and createdate is only updated from filename, if not set; true: tags are always updated
setdate=$setdate [false|true] false: do not set date-tags, only if you are sure, tags are ok, and you want move or copy files;
# destination
dest=$dest
mimedestpic=$mimedestpic
mimedestvid=$mimedestvid
subdestpic=$subdestpic
subdestvid=$subdestvid
# final destinationpath
${dest}/${mimedestpic}/${subdestpic}/%Y/%Y_%m"
${dest}/${mimedestvid}/${subdestvid}/%Y/%Y_%m"
Supported Filet-extensions
*.jpg, *.jpeg, *.png, *.mp4
Examples:
# Scan current directory for all jpg|jpeg|mp4|png files, set datetimeoriginal and createdate
$(basename $0)
# set datetimeoriginal and createdate from given file (only one is possible)
# only if tags are not set
$(basename $0) <filename>
# set datetimeoriginal and createdate from given file (only one is possible)
# overwrite existing tags
forcedate=true $(basename $0) <filename>
# set datetimeoriginal and createdate from given file (only one is possible)
# overwrite existing tags
# move file to destination dir (${dest}/${mimedestpicdefault}|${mimedestviddefault}) in subdirectories %Y/%Y_%m/.
move=true forcedate=true $(basename $0) <filename>
EOF
}
export FILELOGLEVEL=WARN
export SCRIPT_LOG=~/logs/exif.log
parsedatetime() {
filename="$(basename "${@}")"
# 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
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
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
fi
if [ -z "${result}" ]; then
# Whatsapp-Bilder
# Whatsapp-Videos
if [[ $filename =~ ^(IMG-|VID-)([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
fi
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
echo "FILENAME: $@" >&2
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
fi
echo "$result"
return 0
}
fix_datetime () {
file="${@}"
if $forcedate; 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
}
run_fix () {
file="${@}"
mime="$(file --mime-type "$file" | awk '{print $NF}')"
loginfo "file: $file : mime-type: $mime"
newDateTimeOriginal="$(parsedatetime "$file")"
loginfo "newDateTimeOriginal: $newDateTimeOriginal"
if [ -n "$newDateTimeOriginal" ]; then
case $mime in
image/png)
$setdate && exiftool "-creationtime=$newDateTimeOriginal" -if '(not $creationtime)' "$file"
$setdate && fix_datetime "$file"
MIMEDEST="${mimedestpic}/${subdestpic}"
;;
image/jpeg)
$setdate && fix_datetime "$file"
MIMEDEST="${mimedestpic}/${subdestpic}"
;;
video/mp4|video/m4a)
$setdate && fix_datetime "$file"
MIMEDEST="${mimedestvid}/${subdestvid}"
;;
*)
logwarn "unsupportet mimetype »${mime}« for »${file}«"
;;
esac
fi
loginfo "$(exiftool -filename -createdate -datetimeoriginal "$file")"
case $move in
true)
echo move to ${dest}/${MIMEDEST}/%Y/%Y_%m
loginfo "$(exiftool '-Directory<DateTimeOriginal' -d ${dest}/${MIMEDEST}/%Y/%Y_%m -if '($datetimeoriginal)' "$file" 2>&1 |tee -a dest_exists)"
;;
copy)
echo copy to ${dest}/${MIMEDEST}/%Y/%Y_%m
loginfo "$(exiftool -o . '-Directory<DateTimeOriginal' -d ${dest}/${MIMEDEST}/%Y/%Y_%m -filename -createdate -datetimeoriginal -if '($datetimeoriginal)' "$file")"
logwarn "$(readlink -f "$file"): $(exiftool -filename -if '(not ($datetimeoriginal or $createdate))' "$file")"
;;
*)
logwarn "value for move is neither »true« nor »copy«"
esac
loginfo "----------------"
loginfo ""
}
if [ $# -gt 0 ]; then
case $1 in
-h|help|--help)
help
;;
*)
loginfo "Progress only one file from cmdline ${@}"
run_fix "${@}"
;;
esac
else
loginfo "Progress all *.jpg, *.jpeg, *.png and mp4 files in pwd $(pwd)"
all=$(find -maxdepth ${maxdepth-1} -type f |grep -i ".jpg$\|.jpeg$\|.mp4$\|.png$"|wc -l)
find -maxdepth ${maxdepth-1} -type f |grep -i ".jpg$\|.jpeg$\|.mp4$\|.png$"|while read file; do
let "i=i+1"
loginfo "$i/$all"
run_fix "$file"
done
fi