Last active
September 29, 2022 00:17
-
-
Save kobapan/ae5ed3d47116ae6715402a67e3923854 to your computer and use it in GitHub Desktop.
Show or Update the exif ImageDescription and DateTimeOriginal in command line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# exif.sh | |
# usage | |
usage_exit() { | |
if [[ $1 ]]; then echo "error $1"; fi | |
echo "--- | |
Usage: | |
$CMDNAME [-d \"DECRIPTION\"] [-t \"YYYY:MM:DD HH:MM:SS\"] FILENAME [FILENAME ...] | |
FILENAME: .*\.(jpg|jpeg|JPG|JPEG) | |
-d DECRIPTION: this will be written in ImageDescription field | |
-t : DateTimeOriginal field. Default value is CreateDate | |
For malti files, use `-` pipe indicator | |
ls | $CMDNAME -d \"DECRIPTION\" [-t \"YYYY:MM:DD HH:MM:SS\"] - | |
ls should return list of .*\.(jpg|jpeg|JPG|JPEG) with space delimiter | |
--- | |
"; | |
OPTIND=$OPTIND_OLD | |
exit 1 | |
} | |
# init | |
CMDNAME=`basename $0` | |
OPTIND_OLD=$OPTIND | |
OPTIND=1 | |
# option | |
while getopts d:t:h OPT | |
do | |
case $OPT in | |
d ) DESC=$OPTARG ;; | |
t ) DATE=$OPTARG | |
OPT_DATE=1 ;; | |
h ) usage_exit ;; | |
esac | |
done | |
# 変数 OPTIND には次に処理する引数の番号が格納されている | |
# 「処理したオプションの個数」だけ shiftで 位置パラメータの内容をずらす | |
shift `expr $OPTIND - 1` | |
# shift $(($OPTIND - 1)) 上と同じ処理 | |
if [[ ! $1 = "-" ]] && [[ ! -f $1 ]]; then usage_exit "option" ; fi | |
if [[ $1 = "-" ]] ; then # [[ ... ]] 引数有無判定のメッセ消し | |
files=$(cat) | |
else | |
files=$@ | |
fi | |
# main | |
for FILE in $files | |
do | |
if [ -s $FILE ] && [[ $FILE =~ ^.*\.(jpg|JPG|jpeg|JPEG)$ ]] ; then #クォートはマルチバイト文字対応 | |
if [ ! $DESC ] ; then | |
exiftool $FILE | grep -i -E "Image.*Description|Date.*Time.*Original|Create.*Date" | |
else | |
if [ ! $OPT_DATE ] ; then #クォートは空白文字対応 | |
D=`exiftool -CreateDate $FILE` | |
if [[ $D ]] ; then | |
DATE=${D#*: } #左端からのパターン一致除外 | |
else | |
DATE=$(date '+%Y:%m:%d %T') | |
fi | |
fi | |
echo " $FILE"; | |
echo " ImageDescription=$DESC"; | |
echo " DateTimeOriginal=$DATE"; | |
exiftool -ImageDescription="$DESC" -DateTimeOriginal="$DATE" -overwrite_original $FILE | |
fi | |
else | |
usage_exit "file: $FILE" | |
fi | |
done | |
# cleaner | |
OPTIND=$OPTIND_OLD |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment