myshellconfig/bin/exif-fixdatetime

337 lines
15 KiB
Text
Raw Normal View History

2020-11-10 17:26:25 +01:00
#!/bin/bash
2020-11-13 00:21:47 +01:00
2022-01-27 18:20:05 +01:00
[ type loginfo > /dev/null 2>&1 ] || . $(dirname $0)/../logging
2020-11-13 00:21:47 +01:00
[ -z "${move+x}" ] && move=false
2020-11-14 18:20:44 +01:00
[ -z "${forcedate+x}" ] && forcedate=false
[ -z "${setdate+x}" ] && setdate=true
[ -z "${setoriginalfilename+x}" ] && setoriginalfilename=false
2020-11-27 19:38:57 +01:00
[ -z "${movecreatedate+x}" ] && movecreatedate=false
2020-12-13 15:32:10 +01:00
[ -z "${setartist+x}" ] && setartist=false
# Pathdefinitions Default
2020-11-14 18:20:44 +01:00
[ -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="Familie"
2020-11-14 18:20:44 +01:00
# Pathdefinitions Runtime
2020-11-14 18:20:44 +01:00
[ -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]] [setoriginalfilename=[false|true|force]] [<file>]
2020-11-14 18:20:44 +01:00
Defaults:
# commands
2020-11-27 19:38:57 +01:00
move=$move [false|true|copy|rename|renameinplace|copyrename]
false: file is not moved to destination;
true: file is moved; copy: copy file to destination
copy: copy file to final destinateion
renameinplace: rename file to YYYYMMDD_HHmmss-c.ext in place (just rename file)
rename: move like true, but rename ist like renameinplace
copyrename: rename file in place and copy it to final destination with new name
2021-01-09 10:42:31 +01:00
forcedate=$forcedate [false|true] false: datetimeoriginal and createdate is only updated from filename, if not set; true: tags are always updated
2020-11-14 18:20:44 +01:00
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;
2021-01-09 10:42:31 +01:00
setoriginalfilename=$setoriginalfilename [false|true|force] false: do not set originalfilename from filname (default); true: set it, if not set; force: set it, also if exists;
2020-12-13 15:32:10 +01:00
setartist=$setartist [false|true|<User Name>] true: set USERNAME from user, running the script; <User Name> set this User Name
2020-11-14 18:20:44 +01:00
# destination
dest=$dest
mimedestpic=$mimedestpic
mimedestvid=$mimedestvid
2021-01-09 10:42:31 +01:00
2020-11-14 18:20:44 +01:00
subdestpic=$subdestpic
subdestvid=$subdestvid
2021-01-09 10:42:31 +01:00
# or set subdestdefault, which is default for subdestpic and subdestvid
subdestdefault=$subdestdefault
2020-11-14 18:20:44 +01:00
# final destinationpath
${dest}/${mimedestpic}/${subdestpic}/%Y/%Y_%m"
${dest}/${mimedestvid}/${subdestvid}/%Y/%Y_%m"
Supported Filet-extensions
2021-01-09 10:39:41 +01:00
*.jpg, *.jpeg, *.png, *.mp4, *.mov, *.3gp, *.gif
2020-11-14 18:20:44 +01:00
Examples:
2021-01-09 10:39:41 +01:00
# Scan current directory for all jpg|jpeg|mp4|mov|png|3gp|gif files, set datetimeoriginal and createdate
2020-11-14 18:20:44 +01:00
$(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}/${subdestpic} or ${dest}/${mimedestviddefault}/${subdestvid}) in subdirectories %Y/%Y_%m/.
2020-11-14 18:20:44 +01:00
move=true forcedate=true $(basename $0) <filename>
# Final command to move File
exiftool -o . '-Directory<DateTimeOriginal' -d ${dest}/${mimedestpic}/${subdestpic}/%Y/%Y_%m -filename -createdate -datetimeoriginal -if '(\$datetimeoriginal)' <file>
exiftool -o . '-Directory<DateTimeOriginal' -d ${dest}/${mimedestvid}/${subdestvid}/%Y/%Y_%m -filename -createdate -datetimeoriginal -if '(\$datetimeoriginal)' <file>
2020-11-14 18:20:44 +01:00
EOF
}
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
# 2020.04.02_15-32-20+02:00.jpg
# 2020.04.13_10-59-14+02:00.jpg
# WhatsApp Image 2019-07-29 at 12.45.02.jpeg
2022-01-27 18:20:05 +01:00
echo fn: $filename >&2
if [[ $filename =~ ^.*([0-9]{4})(\.|-)([0-9]{2})(\.|-)([0-9]{2})( |_|-|" at ")([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-27 19:38:57 +01:00
if [ -z "${result}" ]; then
# signal-2020-08-09-171936.jpg
2021-08-18 08:30:00 +02:00
if [[ $filename =~ ^.*([0-9]{4})(\.|-)([0-9]{2})(\.|-)([0-9]{2})( |_|-|" at ")([0-9]{2})([0-9]{2})([0-9]{2})(\.|-|\+|_|\(| ).*$ ]]; then
2020-11-27 19:38:57 +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[8]#0}" "${BASH_REMATCH[9]#0}")"
fi
fi
2020-11-13 22:05:51 +01:00
if [ -z "${result}" ]; then
2020-11-14 18:20:44 +01:00
if [[ $filename =~ ^.*([0-9]{4})([0-9]{2})([0-9]{2})(_|-)([0-9]{2})([0-9]{2})([0-9]{2}).*$ ]]; then
2020-11-13 22:05:51 +01:00
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
2020-11-14 18:20:44 +01:00
# Whatsapp-Videos
if [[ $filename =~ ^(IMG-|VID-)([0-9]{4})([0-9]{2})([0-9]{2})(-WA)([0-9]{2})([0-9]{2}).*$ ]]; then
2020-11-13 22:05:51 +01:00
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
2020-11-14 18:20:44 +01:00
if [[ "$(dirname ${@})" =~ ^.*/([0-9]{4})(\.|-)([0-9]{2})(\.|-)([0-9]{2}).*$ ]]; then
2020-11-13 22:05:51 +01:00
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-27 19:38:57 +01:00
2020-11-10 17:26:25 +01:00
echo "$result"
return 0
}
2020-11-14 18:20:44 +01:00
run_fix () {
file="${@}"
2020-11-12 18:33:49 +01:00
mime="$(file --mime-type "$file" | awk '{print $NF}')"
2020-11-14 18:20:44 +01:00
loginfo "file: $file : mime-type: $mime"
2020-11-10 17:26:25 +01:00
2020-11-14 18:20:44 +01:00
newDateTimeOriginal="$(parsedatetime "$file")"
loginfo "newDateTimeOriginal: $newDateTimeOriginal"
case $mime in
image/png)
MIMEDEST="${mimedestpic}/${subdestpic}"
;;
image/jpeg)
MIMEDEST="${mimedestpic}/${subdestpic}"
;;
2020-11-27 19:38:57 +01:00
image/gif)
MIMEDEST="${mimedestpic}/${subdestpic}"
;;
2021-01-09 10:39:41 +01:00
video/mp4|video/m4a|video/3gpp|application/octet-stream)
MIMEDEST="${mimedestvid}/${subdestvid}"
;;
*)
2022-09-14 14:43:45 +02:00
logwarning "unsupportet mimetype »${mime}« for »${file}«"
;;
esac
if [ -n "${MIMEDEST+x}" ] ; then
case $setoriginalfilename in
true)
2020-11-27 19:38:57 +01:00
loginfo "set original filename: "
2020-11-20 13:56:23 +01:00
$setoriginalfilename && exiftool -overwrite_original "-originalfilename<filename" -if '(not $originalfilename)' "$file"
;;
force)
2020-11-27 19:38:57 +01:00
loginfo "set original filename: "
2020-11-20 13:56:23 +01:00
$setoriginalfilename && exiftool -overwrite_original "-originalfilename<filename" "$file"
;;
*)
2022-09-14 14:43:45 +02:00
logwarning "do not set original filname in case of setoriginalfilename=$setoriginalfilename"
;;
esac
2020-12-13 15:32:10 +01:00
case ${setartist:-false} in
false)
unset ARTIST
;;
true)
ARTIST="${FULLNAME:-$(getent passwd $USER|awk -F ":" '{print $5}'|awk -F "," '{print $1}')}"
;;
*)
ARTIST="$setartist"
;;
esac
[ -z "${ARTIST-x}" ] || exiftool -overwrite_original "-artist=$ARTIST" -if '(not $artist)' "$file"
case $forcedate in
true)
loginfo -n "set datetimeoriginal to $newDateTimeOriginal"
$setdate && [ -n "$newDateTimeOriginal" ] && exiftool -overwrite_original "-DateTimeOriginal=$newDateTimeOriginal" "$file"
loginfo -n "set createdate to datetimeoriginal: "
$setdate && exiftool -overwrite_original '-createdate<datetimeoriginal' -if '($datetimeoriginal)' "$file"
loginfo -n "set datetimeoriginal to createdate: "
$setdate && exiftool -overwrite_original '-datetimeoriginal<createdate' -if '(not $datetimeoriginal and $createdate)' "$file"
;;
sensitive)
if [[ $newDateTimeOriginal =~ ^([0-9]{4})(:)([0-9]{2})(:)([0-9]{2})( )([0-9]{2})(:)([0-9]{2})(:)([0-9]{2}).*$ ]]; then
YYYYMM="$(printf "%04d:%02d:*" "${BASH_REMATCH[1]#0}" "${BASH_REMATCH[3]#0}")"
loginfo "YYYYMM: $YYYYMM"
fi
loginfo -n "set datetimeoriginal to $newDateTimeOriginal if not start with $YYYYMM"
$setdate && [ -n "$newDateTimeOriginal" ] && exiftool -overwrite_original "-DateTimeOriginal=$newDateTimeOriginal" -if '(not $DateTimeOriginal =~ "'$YYYYMM'" )' "$file"
loginfo -n "set createdate to datetimeoriginal: "
$setdate && exiftool -overwrite_original '-createdate<datetimeoriginal' -if '($datetimeoriginal)' "$file"
loginfo -n "set datetimeoriginal to createdate: "
$setdate && exiftool -overwrite_original '-datetimeoriginal<createdate' -if '(not $datetimeoriginal and $createdate)' "$file"
;;
*)
loginfo -n "set datetimeoriginal to $newDateTimeOriginal"
$setdate && [ -n "$newDateTimeOriginal" ] && exiftool -overwrite_original "-DateTimeOriginal=$newDateTimeOriginal" -if '(not $datetimeoriginal)' "$file"
loginfo -n "set createdate to datetimeoriginal: "
$setdate && exiftool -overwrite_original '-createdate<datetimeoriginal' -if '(($createdate =~ "0000:00:00*" or not $createdate) and $datetimeoriginal)' "$file"
loginfo -n "set datetimeoriginal to createdate: "
$setdate && exiftool -overwrite_original '-datetimeoriginal<createdate' -if '(not $datetimeoriginal and $createdate)' "$file"
;;
esac
2020-11-20 13:56:23 +01:00
loginfo "fileinfos from modified file: $(exiftool -filename -createdate -datetimeoriginal -originalfilename "$file")"
case $move in
true)
2021-07-10 03:10:10 +02:00
export UMASK=002
echo move to ${dest}/${MIMEDEST}/%Y/%Y_%m
2020-11-27 19:38:57 +01:00
if $movecreatedate ; then
loginfo "$(exiftool '-Directory<CreateDate' -d ${dest}/${MIMEDEST}/%Y/%Y_%m -if '($CreateDate)' "$file" 2>&1 |tee -a ${LOGFILE})"
else
loginfo "$(exiftool '-Directory<DateTimeOriginal' -d ${dest}/${MIMEDEST}/%Y/%Y_%m -if '($datetimeoriginal)' "$file" 2>&1 |tee -a ${LOGFILE})"
fi
2020-11-13 22:05:51 +01:00
;;
copy)
2021-07-10 03:10:10 +02:00
export UMASK=002
echo copy to ${dest}/${MIMEDEST}/%Y/%Y_%m
2020-11-27 19:38:57 +01:00
if $movecreatedate ; then
loginfo "$(exiftool -o . '-Directory<DateTimeOriginal' -d ${dest}/${MIMEDEST}/%Y/%Y_%m -if '($datetimeoriginal)' "$file" 2>&1 |tee -a ${LOGFILE})"
2022-09-14 14:43:45 +02:00
logwarning "$(readlink -f "$file"): $(exiftool -filename -if '(not ($datetimeoriginal or $createdate))' "$file")"
2020-11-27 19:38:57 +01:00
else
loginfo "$(exiftool -o . '-Directory<DateTimeOriginal' -d ${dest}/${MIMEDEST}/%Y/%Y_%m -if '($datetimeoriginal)' "$file" 2>&1 |tee -a ${LOGFILE})"
2022-09-14 14:43:45 +02:00
logwarning "$(readlink -f "$file"): $(exiftool -filename -if '(not ($datetimeoriginal or $createdate))' "$file")"
2020-11-27 19:38:57 +01:00
fi
;;
renameinplace)
echo rename inplace
if $movecreatedate ; then
loginfo "$(exiftool '-FileName<CreateDate' -d %Y%m%d_%H%M%S%%-c.%%e -if '($CreateDate)' "$file" 2>&1 | tee -a ${LOGFILE})"
else
loginfo "$(exiftool '-FileName<DateTimeOriginal' -d %Y%m%d_%H%M%S%%-c.%%e -if '($DateTimeOriginal)' "$file" 2>&1 | tee -a ${LOGFILE})"
fi
;;
rename)
2021-07-10 03:10:10 +02:00
export UMASK=002
2020-11-27 19:38:57 +01:00
echo move to ${dest}/${MIMEDEST}/%Y/%Y_%m
if $movecreatedate ; then
loginfo "$(exiftool -v '-FileName<CreateDate' -d ${dest}/${MIMEDEST}/%Y/%Y_%m/%Y%m%d_%H%M%S%%-c.%%e -if '($CreateDate)' "$file" 2>&1 | tee -a ${LOGFILE})"
else
loginfo "$(exiftool -v '-FileName<DateTimeOriginal' -d ${dest}/${MIMEDEST}/%Y/%Y_%m/%Y%m%d_%H%M%S%%-c.%%e -if '($DateTimeOriginal)' "$file" 2>&1 | tee -a ${LOGFILE})"
fi
;;
copyrename)
2021-07-10 03:10:10 +02:00
export UMASK=002
2020-11-27 19:38:57 +01:00
echo move to ${dest}/${MIMEDEST}/%Y/%Y_%m
if $movecreatedate ; then
loginfo "$(exiftool -o . '-FileName<CreateDate' -d ${dest}/${MIMEDEST}/%Y/%Y_%m/%Y%m%d_%H%M%S%%-c.%%e -if '($CreateDate)' "$file" 2>&1 | tee -a ${LOGFILE})"
else
loginfo "$(exiftool -o . '-FileName<DateTimeOriginal' -d ${dest}/${MIMEDEST}/%Y/%Y_%m/%Y%m%d_%H%M%S%%-c.%%e -if '($DateTimeOriginal)' "$file" 2>&1 | tee -a ${LOGFILE})"
fi
2020-11-12 18:33:49 +01:00
;;
*)
2022-09-14 14:43:45 +02:00
logwarning "value for move is neither »true« nor »copy«"
2020-11-12 18:33:49 +01:00
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 "----------------"
loginfo ""
2020-11-10 17:26:25 +01:00
}
2020-12-13 15:32:10 +01:00
run_check () {
file="${@}"
mime="$(file --mime-type "$file" | awk '{print $NF}')"
loginfo "file: $file : mime-type: $mime"
newDateTimeOriginal="$(parsedatetime "$file")"
loginfo "search for DateTime-File: $newDateTimeOriginal"
#echo $newDateTimeOriginal|sed -e 's/://g;s/ /_/'
if [ $(find ${dest} -name "$(echo $newDateTimeOriginal|sed -e 's/://g;s/ /_/')*"|wc -l) -eq 0 ]; then
loginfo " not found"
echo "$@" |tee -a holdfiles
else
loginfo " found"
echo "$@" |tee -a rmfiles
fi
}
2020-11-10 17:26:25 +01:00
if [ $# -gt 0 ]; then
2020-11-14 18:20:44 +01:00
case $1 in
-h|help|--help)
help
;;
2020-12-13 15:32:10 +01:00
-c)
shift
loginfo "Check if createdate-file already exists"
run_check "${@}"
;;
2020-11-14 18:20:44 +01:00
*)
loginfo "Progress only one file from cmdline ${@}"
run_fix "${@}"
;;
esac
2020-11-10 17:26:25 +01:00
else
2021-01-09 10:39:41 +01:00
loginfo "Progress all *.jpg, *.jpeg, *.png and mp4, *.3gp, *.gif *.mov files in pwd $(pwd)"
all=$(find ${maxdepth:+-maxdepth} ${maxdepth:+$maxdepth} -type f |grep -i ".jpg$\|.jpeg$\|.mp4$\|.mov$\|.png$\|.3gp$\|.gif$"|wc -l)
find ${maxdepth:+-maxdepth} ${maxdepth:+$maxdepth} -type f |grep -i ".jpg$\|.jpeg$\|.mp4$\|.mov$\|.png$\|.3gp$\|.gif$"|while read file; do
2020-11-14 18:20:44 +01:00
let "i=i+1"
2021-07-12 05:03:30 +02:00
export SCRIPT_LOG=./$(basename $0).log
export LOGFILE=./$(basename $0).move.log
2020-11-14 18:20:44 +01:00
loginfo "$i/$all"
run_fix "$file"
2020-11-10 17:26:25 +01:00
done
fi