Skip to content

Instantly share code, notes, and snippets.

@oPromessa
Last active October 20, 2018 15:33
Show Gist options
  • Save oPromessa/059766e466df88383493f40a261e8391 to your computer and use it in GitHub Desktop.
Save oPromessa/059766e466df88383493f40a261e8391 to your computer and use it in GitHub Desktop.
Helpful bash exiftool related functions to set various EXIF and date/time values on pics and movies files.
###############################################################################
#
# EXIFTOOL related commands
#
###############################################################################
if [ ! -f ~/EXIF_format.fmt ]
then
cat > ~/EXIF_format.fmt << EOF
#[HEAD]Directory|FileName|Make|DateTimeOriginal|FileModifyDate|CreateDate|Keywords|Subject|ImageOrientation|Caption-Abstract|ModifyDate|Description
\$Directory|\$FileName|\$Make|\$DateTimeOriginal|\$FileModifyDate|\$CreateDate|\$Keywords|\$Subject|\$ImageOrientation|\$Caption-Abstract|\$ModifyDate|\$Description
EOF
fi
#==============================================================================
# Shows EXIF info and Date/Time info
#------------------------------------------------------------------------------
function exif() {
exiftool -r -d """%Y:%m:%d %H:%M:%S""" -p """~/EXIF_format.fmt""" -f "$@"
}
export -f exif
#==============================================================================
# Sets CreateDate, FileModifyDate and ModifyDate (OS related) based on EXIF OriginalDate
#------------------------------------------------------------------------------
function exiforiginaldate () {
echo Performing... """-CreateDate\<DateTimeOriginal""" """-FileModifyDate\<DateOriginalDate"""
exiftool -d """%Y:%m:%d %H:%M:%S""" -overwrite_original_in_place -fileOrder DateTimeOriginal """-CreateDate<DateTimeOriginal""" """-FileModifyDate<DateTimeOriginal""" """-ModifyDate<DateTimeOriginal""" -v "$@"
}
export -f exiforiginaldate
#==============================================================================
# Sets OriginalDate and FileModifyDate based on EXIF CreateDate
#------------------------------------------------------------------------------
function exifcreatedate (){
echo Performing... ""-DateTimeOriginal\<CreateDate"" ""-FileModifyDate\<CreateDate""
exiftool -d """%Y:%m:%d %H:%M:%S""" -overwrite_original_in_place -fileOrder DateTimeOriginal """-DateTimeOriginal<CreateDate""" """-FileModifyDate<CreateDate""" -v "$@"
}
export -f exifcreatedate
#==============================================================================
# Sets OriginalDate and CreateDate based on EXIF FileModifyDate
#------------------------------------------------------------------------------
function exifmodifydate () {
echo Performing... ""-DateTimeOriginal\<FileModifyDate"" ""-CreateDate\<FileModifyDate""
exiftool -overwrite_original_in_place """-DateTimeOriginal<FileModifyDate""" """-CreateDate<FileModifyDate""" """-ModifyDate<FileModifyDate""" -P -v "$@"
}
export -f exifmodifydate
#==============================================================================
# Convert a .PNG (limited EXIF support) to .JPG while copying as many tags as
# possible.
#------------------------------------------------------------------------------
function exifpng2jpg() {
# Command which convert .PNG files into .JPG and copies EXIF fields ...
for a in "$@"
do
fname=`basename "$a" .png`
xtension=png
echo 1st - ${fname}.${xtension}
if [ \( -f "${fname}".png -o -f "${fname}".PNG \) -a ! -f "${fname}".jpg ]
then
echo ok - ${fname}.${xtension}
sips -s format jpeg -s formatOptions 90 """${fname}".${xtension}"" --out """${fname}".jpg""
exiftool -overwrite_original_in_place -TagsFromFile "${fname}".${xtension} "-all:all>all:all" "${fname}".jpg
else
fname=`basename "$a" .PNG`
xtension=PNG
echo 2nd - ${fname}.${xtension}
if [ \( -f "${fname}".png -o -f "${fname}".PNG \) -a ! -f "${fname}".jpg ]
then
echo ok - ${fname}.${xtension}
sips -s format jpeg -s formatOptions 90 """${fname}".${xtension}"" --out """${fname}".jpg""
exiftool -overwrite_original_in_place -TagsFromFile "${fname}".${xtension} "-all:all>all:all" "${fname}".jpg
fi
fi
done
}
export -f exifpng2jpg
#==============================================================================
# Convert a .TIF (limited EXIF support) to .JPG while copying as many tags as
# possible.
#------------------------------------------------------------------------------
function exiftif2jpg() {
# Command which convert .TIF files into .JPG and copies EXIF fields ... Uses ModifyDate as the source for DateTimeOriginal, FileModifyDate, CreateDate fields
for a in "$@"
do
fname=`basename "$a" .tif`
dname=`dirname "$a" `
if [ -f "${dname}/${fname}".tif -a ! -f "${dname}/${fname}".jpg ]
then
echo ok - ${fname}
sips -s format jpeg -s formatOptions 100 """${dname}/${fname}".tif"" --out """${dname}/${fname}".jpg""
exiftool -overwrite_original_in_place -TagsFromFile "${dname}/${fname}".tif "-all:all>all:all" "${dname}/${fname}".jpg
echo Performing... ""-DateTimeOriginal\<FileModifyDate"" ""-CreateDate\<FileModifyDate"" on "${dname}/${fname}".jpg
exiftool -overwrite_original_in_place """-DateTimeOriginal<ModifyDate""" """-CreateDate<ModifyDate""" """-FileModifyDate<ModifyDate""" -P -v "${dname}/${fname}".jpg
fi
done
}
export -f exiftif2jpg
#==============================================================================
# Sets OriginalDate
#------------------------------------------------------------------------------
function exifsetdate() {
datetoset=${1}; echo $datetoset
shift
exiftool -d """%Y:%d:%m %H:%M:%S""" -overwrite_original_in_place -fileOrder DateTimeOriginal -DateTimeOriginal="""${datetoset}""" -v "$@"
}
export -f exifsetdate
#==============================================================================
# Sets CreateDate
#------------------------------------------------------------------------------
function exifresetcreatedate() {
datetoset=${1}; echo $datetoset
shift
exiftool -d """%Y:%d:%m %H:%M:%S""" -overwrite_original_in_place -fileOrder DateTimeOriginal -CreateDate="""${datetoset}""" -v "$@"
}
export -f exifresetcreatedate
#==============================================================================
# Sets ModifyDate
#------------------------------------------------------------------------------
function exifresetmodifydate() {
datetoset=${1}; echo $datetoset
shift
exiftool -d """%Y:%d:%m %H:%M:%S""" -overwrite_original_in_place -fileOrder DateTimeOriginal -FileModifyDate="""${datetoset}""" -v "$@"
}
export -f exifresetmodifydate
#==============================================================================
# Sets OriginalDate, FileModifyDate and CreateDate
#------------------------------------------------------------------------------
function exifsetalldates() {
datetoset=${1}; echo $datetoset
shift
exiftool -d """%Y:%d:%m %H:%M:%S""" -overwrite_original_in_place -fileOrder DateTimeOriginal -DateTimeOriginal="""${datetoset}""" -CreateDate="""${datetoset}""" -FileModifyDate="""${datetoset}""" -ModifyDate="""${datetoset}""" -v "$@"
}
export -f exifsetalldates
#==============================================================================
# Renames a file based on OriginalDate
#------------------------------------------------------------------------------
function exifrenamefile() {
exiftool -v4 -r -d """IMG_%Y%m%d_%H%M%S%%-c.%%le""" '-filename<DateTimeOriginal' -f "$@"
echo "Note: Do you need to run exiforiginaldate?"
}
export -f exifrenamefile
#==============================================================================
# exifaddsecs()
# - number of seconds to add
# exiftool -overwrite_original_in_place '-DateTimeOriginal+<0:$filesequence' -v *.JPG
#------------------------------------------------------------------------------
function exifaddsecs() {
E_BADARGS=85
E_BADFILES=90
if [ ! -n "$1" ]
then
echo "Usage: exifaddsecs secs_to_add dstfile"
echo "Usage: secs_to_add between 1 and 59 to DateTimeOriginal"
echo "Usage: use exiforiginaldate afterwards as appropriate"
return $E_BADARGS
elif [ "$1" -gt 0 -a "$1" -lt 60 ]
then
secstoadd=${1}; echo $secstoadd
shift
echo Adding "${secstoadd}" to "$@"
# with * it multiplies the filesequence by X seconds... so it is a geometric progression!
# exiftool -overwrite_original_in_place '-datetimeoriginal+<0:0:${filesequence;$_*='$secstoadd'}' -v "$@"
# exiftool -overwrite_original_in_place '-datetimeoriginal+<0:0:${filesequence;$_='$secstoadd'}' -v "$@"
exiftool -overwrite_original_in_place '-datetimeoriginal+<0:0:${filesequence;$_*='$secstoadd'}' -v "$@"
else
echo "Usage: exifaddsecs secs_to_add dstfile"
echo "Usage: secs_to_add between 1 and 59"
echo "Usage: use exiforiginaldate afterwards as appropriate"
return $E_BADFILES
fi
}
export -f exifaddsecs
#==============================================================================
# exifcopytags()
# - srcfile
# - dstfile
# Copy all metadata from one file to another
# exiftool -TagsFromFile srcimage.jpg "-all:all>all:all" targetimage.jpg
# USE -overwrite_original_in_place in applicable
# USE WITHOUT all:all will copy everything including GPS data (works with MP4)
#------------------------------------------------------------------------------
function exifcopytags() {
E_BADARGS=85
E_BADFILES=90
if [ ! -n "$1" -o ! -n "$2" ]
then
echo "Usage: exifcopytags srcfile dstfile"
return $E_BADARGS
elif [ -f "$1" ]
then
srcfile=${1}; shift
echo Copying all tags from "${srcfile}" to "$@"
exiftool -v -overwrite_original_in_place -TagsFromFile "${srcfile}" "${@}"
else
echo "Usage: file(s) not found!"
echo "Usage: exifcopytags srcfile dstfile"
return $E_BADFILES
fi
}
export -f exifcopytags
#==============================================================================
# exifcleantags()
# - files
# Clean all metadata from one file
# exiftool -overwrite_original -all= -gps:all= *.jpg
# USE -overwrite_original_in_place in applicable
#------------------------------------------------------------------------------
function exifcleantags() {
E_BADARGS=85
E_BADFILES=90
if [ ! -n "$1" ]
then
echo "Usage: exifcleantags files"
return $E_BADARGS
elif [ -f "$1" ]
then
echo Cleaning all tags from "$@"
exiftool -v -overwrite_original -all= -gps:all= "${@}"
else
echo "Usage: file(s) not found!"
echo "Usage: exifcleantags files"
return $E_BADFILES
fi
}
export -f exifcleantags
#[HEAD]Directory|FileName|Make|DateTimeOriginal|FileModifyDate|CreateDate|Keywords|Subject|ImageOrientation|Caption-Abstract|ModifyDate|Description
$Directory|$FileName|$Make|$DateTimeOriginal|$FileModifyDate|$CreateDate|$Keywords|$Subject|$ImageOrientation|$Caption-Abstract|$ModifyDate|$Description
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment