Skip to content

Instantly share code, notes, and snippets.

@bluPhy
Last active December 27, 2022 20:37
Show Gist options
  • Save bluPhy/e506bccb0639cfcce25a808b2e3c7ab9 to your computer and use it in GitHub Desktop.
Save bluPhy/e506bccb0639cfcce25a808b2e3c7ab9 to your computer and use it in GitHub Desktop.
exif_datetime_setter.sh
#!/bin/bash
# Orginal script from pirafrank https://gist.github.com/pirafrank/9970c15fec0fa6e49be5363ef6ed1cf3
# Simple script to parse JPG photo filename and set exif datetime,
# creation date and file modification date accordingly.
# Timezone is taken by your PC.
# exec this script by searching for .JPG and .jpg files in given folder
# find . -type f -name "*.JPG" -o -name "*.jpg" -exec /path/to/exif_datetime_setter.sh {} \;
# Force update of all files based on exif info -> exiftool '-FileModifyDate<Exif:CreateDate' -overwrite_original *
# Force update of all files based on exif info -> exiftool '-AllDates<Exif:CreateDate' -overwrite_original *
# For videos -> exiftool '-FileModifyDate<Track1: MediaCreateDate' -overwrite_original -ext mp4 -r .
force="false"
if [ -z "$1" ]; then
echo "Error: no input file as argument. Exiting..."
exit 1
fi
filepath="$1"
filename="${1##*/}"
# fotor pattern exception as it puts the time in epoch format
fotor_re="(fotor)(_|-)([0-9]{10})([0-9]{4,5})(\.)(.{3})"
# Windows Phone pattern exception as it does not put the time in filename
WP_re="(WP)(-?|_?)([0-9]{4})([0-9]{2})([0-9]{2})(-?|_?)([0-9]{3})(\.)(.{3})"
# Filename match pattern for files starting with just numbers
# 20160624_040232162_iOS.jpg
# 20130615_005110.jpg
filename_num_re="([0-9]{4})(-?|_?)(1[0-2]|0[1-9])(-?|_?)(0[1-9]|[12][0-9])(-?|_?)([0-9]{2})(-?|_?)([0-9]{2})(-?|_?)([0-9]{2})(.*)(\.)(.{3,4})"
# Filename match pattern for: Screenshot_20160213-224012.png
# IMG_2021-12-10_060707.png
# WP_2022-12-10_060707.png
# WP_20160104_16_18_34_Pro_LI.png
# IMG_20160216_183300.jpg
#filename_re="([a-zA-Z0-9_]*|[^a-zA-Z0-9_]*)([0-9]{4})(-?|_?)([0-9]{2})(-?|_?)([0-9]{2})(-?|_?)([0-9]{2})(-?|_?)([0-9]{2})(-?|_?)([0-9]{2})(.*)(\.)(.{3,4})"
filename_re="([a-zA-Z0-9_]*|[^a-zA-Z0-9_]*)([0-9]{4})(-?|_?)(1[0-2]|0[1-9])(-?|_?)(0[1-9]|[12][0-9])(-?|_?)([0-9]{2})(-?|_?)([0-9]{2})(-?|_?)([0-9]{2})(.*)(\.)(.{3,4})"
if [[ $filename =~ $fotor_re ]]; then
epoctime=${BASH_REMATCH[3]}
arg2=$(date -r "$epoctime" +%Y:%m:%d\ %H:%M:%S)
echo Changing Fotor file "$filename" datetimeoriginal to: "$arg2"
elif [[ $filename =~ $WP_re ]]; then
year=${BASH_REMATCH[3]}
month=${BASH_REMATCH[4]}
day=${BASH_REMATCH[5]}
hour="12"
mins="00"
secs="00"
arg2="$year:$month:$day $hour:$mins:$secs"
echo Changing WP file "$filename" datetimeoriginal to: "$arg2"
elif [[ $filename =~ $filename_num_re ]]; then
echo "Working on: $filepath ..."
year=${BASH_REMATCH[1]}
month=${BASH_REMATCH[3]}
day=${BASH_REMATCH[5]}
hour=${BASH_REMATCH[7]}
mins=${BASH_REMATCH[9]}
secs=${BASH_REMATCH[11]}
arg2="$year:$month:$day $hour:$mins:$secs"
echo Changing Number: "$filename" datetimeoriginal to: "$arg2"
elif [[ $filename =~ $filename_re ]]; then
echo "Working on: $filepath ..."
year=${BASH_REMATCH[2]}
month=${BASH_REMATCH[4]}
day=${BASH_REMATCH[6]}
hour=${BASH_REMATCH[8]}
mins=${BASH_REMATCH[10]}
secs=${BASH_REMATCH[12]}
arg2="$year:$month:$day $hour:$mins:$secs"
echo Changing "$filename" datetimeoriginal to: "$arg2"
else
echo "Error: $filename does not match pattern. Exiting..."
exit 1
fi
# from exiftool docs:
# "If the Geotime tag is not specified, the value of DateTimeOriginal
# is used for geotagging. Local system time is assumed unless
# DateTimeOriginal contains a timezone."
if [ "$force" = "true" ] ; then
exiftool -overwrite_original -DateTimeOriginal="$arg2" -filemodifydate="$arg2" \
-FileCreateDate="$arg2" -createdate="$arg2" -modifydate="$arg2" "$filepath"
exit 0
else
# if datetimeoriginal is set, set file modification and file creation dates to datetimeoriginal value
exiftool -if 'defined $datetimeoriginal and not $datetimeoriginal =~ /(^\s*$)/' \
"-FileCreateDate<DateTimeOriginal" "-FileModifyDate<DateTimeOriginal" "$filepath"
# if datetimeoriginal is missing, is empty or contains only spaces, set datetimeoriginal from data parsed from filename
exiftool -if 'not defined $datetimeoriginal or $datetimeoriginal =~ /(^\s*$)/' \
-overwrite_original -DateTimeOriginal="$arg2" -filemodifydate="$arg2" \
-FileCreateDate="$arg2" -createdate="$arg2" -modifydate="$arg2" "$filepath"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment