gestion-de-commission/Convention photos/Traitements/rphoto

88 lines
2.0 KiB
Plaintext
Raw Permalink Normal View History

2022-07-19 15:33:35 +02:00
#!/bin/bash
2022-07-07 23:09:06 +02:00
2022-07-08 02:00:59 +02:00
checkcommand()
2022-07-07 23:09:06 +02:00
{
local result
2022-07-08 02:00:59 +02:00
local command="$1"
which "$command" 1>/dev/null 2>&1
if [[ $? == 0 ]]; then
echo "$command requirement OK"
2022-07-07 23:09:06 +02:00
result=1
else
2022-07-08 02:00:59 +02:00
echo "$command requirement MISSING"
2022-07-07 23:09:06 +02:00
result=0
fi
return $result
}
2022-07-08 02:00:59 +02:00
help()
{
2022-08-20 21:08:28 +02:00
echo "Usage: rphoto [ -h | -f ] [ *.jpg ]"
2022-07-19 15:33:35 +02:00
echo "Rename jpg files using Exif Original Date/Time if exists."
echo " -h Display help."
echo " -f Force rename jpg files with Exif datetime or file datetime."
2022-07-08 02:00:59 +02:00
}
2022-07-07 23:09:06 +02:00
2022-07-19 15:33:35 +02:00
doRename()
2022-07-08 02:00:59 +02:00
{
2022-07-19 15:33:35 +02:00
local filename=$(basename $1)
#echo "$filename"
2022-07-21 10:34:36 +02:00
jhead -nimg%Y%m%d-%H%M%S "$filename"
2022-07-08 02:00:59 +02:00
}
2022-07-07 23:09:06 +02:00
2022-07-19 15:33:35 +02:00
doRenameCarefully()
2022-07-08 02:00:59 +02:00
{
2022-07-19 15:33:35 +02:00
local filename=$(basename $1)
#echo "$filename"
2022-07-19 15:53:05 +02:00
if [[ $filename =~ IMG[12][90][0-9][0-9][01][0-9][0-3][0-9][012][0-9][0-5][0-9][0-5][0-9].* ]]; then
2022-07-21 10:34:36 +02:00
local target=img${filename:3:8}-${filename:11}
if [ -e "$target" ]; then
echo "$filename -r> $target ABORT because file already exists."
else
echo "$filename -r> $target"
mv "$filename" "$target"
fi
2022-07-19 15:53:05 +02:00
elif [[ $(exiftool $filename | grep -c "Date/Time Original") > 0 ]]; then
2022-07-19 15:33:35 +02:00
#echo "yop $filename"
doRename "$filename"
else
echo "$filename IGNORED"
fi
2022-07-08 02:00:59 +02:00
}
# main
2022-07-19 15:33:35 +02:00
checkcommand "exiftool"
if [[ $? == 0 ]]; then
exit 1
else
checkcommand "jhead"
if [[ $? == 0 ]]; then
exit 1
fi
fi
2022-07-08 02:00:59 +02:00
if [[ $# == 0 ]]; then
2022-07-19 15:33:35 +02:00
find . -maxdepth 1 -type f -iname "*.jpg" | while read path
do
doRenameCarefully "$path"
done
2022-07-08 02:00:59 +02:00
elif [[ "$1" == "-h" ]]; then
help
elif [[ "$1" == "-f" && $# == 1 ]]; then
2022-07-19 15:33:35 +02:00
find . -maxdepth 1 -type f -iname "*.jpg" | while read path
do
doRename "$path"
done
2022-07-08 02:00:59 +02:00
elif [[ "$1" == "-f" && $# > 1 ]]; then
for filename do
if [[ "$filename" != "-f" ]]; then
2022-07-19 15:33:35 +02:00
doRename "$filename"
2022-07-07 23:09:06 +02:00
fi
done
else
2022-07-08 02:00:59 +02:00
for filename do
2022-07-19 15:33:35 +02:00
doRenameCarefully "$filename"
2022-07-08 02:00:59 +02:00
done
2022-07-07 23:09:06 +02:00
fi
2022-07-19 15:33:35 +02:00