Benutzer-Werkzeuge

Webseiten-Werkzeuge


de:sysadmin:tools:exipicrename

exipicrename - JPG und RAW + Metafiles nach Datum umbenennen

Du hast die Kamera so eingestellt, dass sie JPG + RAW fotografiert … und jetzt willst Du die Bilder nach Datum + Uhrzeit umbennennen. Ausserdem sollen alle schon entstandenen Metadateien (XMP) auch noch gleich umbenannt werden.

Aufruf: exipicrename *.JPG

eine umfangreichere neuere Version dieses Scripts mit bash und exiftool gibt es auf github

und eine noch schönere (und wesentlich schnellere) Variante mit python3 gibts auch auf github: exipicrename2

#!/bin/bash 
 
# exipicrename
 
# rename image files after their exif creation date and time
# AND also rename orf (raw) + xmp (metadata) files with the same new name
 
# if there are images created with the same date+time (more than one
# image per second) add a counter to the name (avoid overwriting).
 
# requires package "exiftool"
# requires package "dateutils" for dateconv
 
 
# parts of filenames
# if there is also a raw file to this jpg, add :
alsoraw=ar
# if the file is a doublicate, mark it with:
duplicatemarker=_DOUBLE_
 
### start
 
opt=$@
 
export LANG=en_US.utf8
 
METAFILEWANTED=
RAWDIR=
NORAWDIR=
getopts ":n" NORAWDIR
getopts ":r" RAWDIR
 
if [[ "$NORAWDIR" == "n" ]]; then
	printf "Option -n (NORAWDIR) specified\n"
	printf "$NORAWDIR\n"
fi
shift $(($OPTIND -1))
 
if [[ "$RAWDIR" == "r" ]]; then
	printf "Option -r (RAWDIR) specified\n"
	printf "$RAWDIR\n"
fi
shift $(($OPTIND -1))
 
alljpg=$*
 
for f in $alljpg; do
 
datetime=`exiftool -p '$DateTimeOriginal' -d %Y%m%d_%H%M%S $f`
### some systembug gives the time unformated in the format 2016:06:10 11:13:58
### so if this happens I need to format that nice in other ways :-(
### (it seems not to be the fault of exiftool but somewhere in import with digikam, kde library or such)
 
### problem exists if there are colons : in the output ...
if [[ $datetime == *":"* ]]; then
	echo timeformat not converted: $datetime
	zwischenzeit=$(dateconv "$datetime" -i "%Y:%m:%d %H:%M:%S")
	datetime=$(date -d"$zwischenzeit" +%Y%m%d_%H%M%S)
	echo timeformat converted: $datetime
 
fi ; # end format $datetime
 
 
dir=`dirname $f`
bn=`basename $f .jpg`
bn=`basename $bn .JPG`
 
rawdir=$dir
if [[ "$NORAWDIR" == "n" ]]; then
	rawdir=$dir
fi
if [[ "$RAWDIR" == "r" ]]; then
	rawdir=$dir/raw
fi
metadir=$dir/meta
 
origname=$dir/$bn
newname=$dir/$datetime
 
if [[ -f $origname.JPG ]]; then
	mv $origname.JPG $origname.jpg
fi
 
# check if there already is a file with this time-name which is not
# the same file ( 2 cameras or serial shot in the same second)
counter=0
 
while ( [[ -f $newname.$alsoraw.jpg ]] ) ; do
	counter=$(( $counter + 1 ))
	echo file $newname.$alsoraw.jpg already there
	diff $origname.jpg $newname.$alsoraw.jpg >/dev/null
  if [[ $? -ne 0 ]]; then
		newname=$dir/${datetime}_$counter
		echo "newname: $newname counter: $counter"
	else
		echo file $origname.JPG is a dublicate of $newname.$alsoraw.jpg
		newname=$dir/${datetime}${duplicatemarker}$counter
	fi
done
 
while ( [[ -f $newname.jpg ]] ) ; do
	counter=$(( $counter + 1 ))
	echo datei $newname.jpg already there
	diff $origname.JPG $newname.jpg >/dev/null
  if [[ $? -ne 0 ]]; then
		newname=$dir/${datetime}_$counter
		echo "neuer name: ${newname}_${counter}"
	else
		echo file $origname.JPG is a dublicate of $newname.jpg
		newname=$dir/${datetime}${duplicatemarker}$counter
	fi
done
 
printf "$f -> $newname\n"
 
# safe meta information (txt and exifinfo)
if [[ $METAFILEWANTED ]]; then
	mkdir -p $metadir
 
	if [[ -f $origname.txt ]]; then
		echo "mv $origname.txt $metadir/$newname.txt"
		mv $origname.txt $metadir/$newname.txt
	fi
 
	metafile=$metadir/$newname.meta0.txt
	if [[ -f $metafile ]]; then
		echo "Metafile $metafile already there" > /dev/stderr
	else
		exiftool $origname.jpg > $metafile
	fi
fi
 
 
if ( [[ $datetime == $bn ]] || [[ $datetime.$alsoraw == $bn ]] ); then
	echo "file $f already correctly renamed" > /dev/stderr
	continue
fi
 
 
if [[ -f $newname.jpg ]]; then
  echo "$newname.jpg is already available ($origname.jpg won't be renamed)" > /dev/stderr
  continue
fi
 
 
# create a directory for raw pictures, if requested
if [[ "$RAWDIR" == "r" ]]; then
	mkdir -p $rawdir
fi
 
# if the directory for raw is not writable, end script
if ! ( [[ -w $rawdir ]] && [[ -d $rawdir ]]); then
	echo "directory $rawdir not writable" > /dev/stderr
	exit 5 
fi
 
# finaly rename everything
 
if [[ -f $origname.orf ]];then
	mv $origname.orf $rawdir/$newname.orf
	mv $origname.jpg $newname.$alsoraw.jpg
	if [[ -f $origname.orf.xmp ]];then
		mv $origname.orf.xmp $rawdir/$newname.orf.xmp
	fi
elif [[ -f $origname.ORF ]];then
	mv $origname.ORF $rawdir/$newname.orf
	mv $origname.jpg $newname.$alsoraw.jpg
	if [[ -f $origname.ORF.xmp ]];then
		mv $origname.ORF.xmp $rawdir/$newname.orf.xmp
	fi
else
	mv $origname.jpg $newname.jpg
fi
 
if [[ -f $origname.JPG.xmp ]] ; then
	mv $origname.JPG.xmp $newname.jpg.xmp
elif [[ -f $origname.jpg.xmp ]] ; then
	mv $origname.jpg.xmp $newname.jpg.xmp
fi
 
done

de/sysadmin/tools/exipicrename.txt · Zuletzt geändert: 2019-09-25 22:01 von hella

Seiten-Werkzeuge

Mastodon Twitter