myshellconfig/bin/exif-fixdatetime
Jakobus Schürz b71290d038 add 3gp
2020-11-19 21:35:26 +01:00

197 lines
7.8 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
# Pathdefinitions Default
[ -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"
# Pathdefinitions Runtime
[ -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}/${subdestpic} or ${dest}/${mimedestviddefault}/${subdestvid}) in subdirectories %Y/%Y_%m/.
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>
EOF
}
export FILELOGLEVEL=WARN
export SCRIPT_LOG=~/logs/exif.log
parsedatetime() {
filename="$(basename "${@}")"
# 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
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
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
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
}
run_fix () {
file="${@}"
mime="$(file --mime-type "$file" | awk '{print $NF}')"
loginfo "file: $file : mime-type: $mime"
newDateTimeOriginal="$(parsedatetime "$file")"
loginfo "newDateTimeOriginal: $newDateTimeOriginal"
case $mime in
image/png)
MIMEDEST="${mimedestpic}/${subdestpic}"
;;
image/jpeg)
MIMEDEST="${mimedestpic}/${subdestpic}"
;;
video/mp4|video/m4a|video/3gpp)
MIMEDEST="${mimedestvid}/${subdestvid}"
;;
*)
logwarn "unsupportet mimetype »${mime}« for »${file}«"
;;
esac
if [ -n "${MIMEDEST+x}" ] ; then
if $forcedate; then
$setdate && [ -n "$newDateTimeOriginal" ] && exiftool "-DateTimeOriginal=$newDateTimeOriginal" "$file"
$setdate && exiftool '-createdate<datetimeoriginal' -if '($datetimeoriginal)' "$file"
$setdate && exiftool '-datetimeoriginal<createdate' -if '(not $datetimeoriginal and $createdate)' "$file"
else
$setdate && [ -n "$newDateTimeOriginal" ] && exiftool "-DateTimeOriginal=$newDateTimeOriginal" -if '(not $datetimeoriginal)' "$file"
$setdate && exiftool '-createdate<datetimeoriginal' -if '(not $createdate and $datetimeoriginal)' "$file"
$setdate && exiftool '-datetimeoriginal<createdate' -if '(not $datetimeoriginal and $createdate)' "$file"
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
fi
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, *.3gp files in pwd $(pwd)"
all=$(find ${maxdepth:+-maxdepth} ${maxdepth:+$maxdepth} -type f |grep -i ".jpg$\|.jpeg$\|.mp4$\|.png$\|.3gp$"|wc -l)
find ${maxdepth:+-maxdepth} ${maxdepth:+$maxdepth} -type f |grep -i ".jpg$\|.jpeg$\|.mp4$\|.png$\|.3gp$"|while read file; do
let "i=i+1"
loginfo "$i/$all"
run_fix "$file"
done
fi