Renaming with exiftool: how to deal with missing tags?
I'm dealing with an unorganized, big group of media files, captured using several different cameras and camcorders, and I'd like to use exiftool in a bash script to rename each file with its creation date and time, maker and model.
With this command images are renamed as I want:
exiftool -m -P '-filename<${DateTimeOriginal}_${Make}_${Model}.%e' myfile.jpg
I already know I can format date and time using -d FMT option, but I'm omitting it for clarity.
Here's instead the problems I'm facing with DSLR "*.mov" videos:
- exiftool is dropping the .mov extension, even if it use ".mov" instead ".%e" in the file name model
- those files don't have the DateTimeOriginal tag, and I have to use the MediaCreateDate tag that other files don't have
Does anyone know how to "force" exiftool to use the original, .mov extension?
Is there a way to tell exiftool to check if an exif tag exists or not? And use another tag if the first isn't there?
I tried with something like
if [ -n `exiftool -m -p '$DateTime' filename]
then
...
But exiftool is dropping me an error, so the empty string check always fails. Any tip?
Thanks to feedback from Phil Harvey, the dev of exiftool, found a really simple solution to my problem, that works with exiftool 9.30.
Phil says here (http://u88.n24.queensu.ca/exiftool/forum/index.php/topic,5079.msg24483.html#msg24483):
The last valid assignment supersedes the others.
So we can do everything with just one command. Here's the final code:
#!/bin/bash
exiftool -P -d '%Y-%m-%d %H.%M.%S' \
'-filename<${CreateDate}_${Model;}.%e' \
'-filename<${DateTimeOriginal}_${Make;}.%e' \
'-filename<${DateTimeOriginal}_${Make;}_${Model;}.%e' \
$@
The semicolon it's a work around to deal with the missing extension problem stated above (actually, it was because of trailing nulls in my Model tags).
One good thing of this approach is that it's that we aren't using the -m
option, so we won't mess up for example *.mov files that don't have exif tags, like most of the videos one can download from the Net (they are just ignored).
As far as I know, exiftool does not support writing the .mov format, it might explain why it does not want to create files with that extension, even if just renaming.
As you need some logic to peek into alternate tags if one of them is empty, I suggest you use exiftool only to extract the info you want; then rename your files yourself. Here is a bash script to get you started with the idea, it won't work out of the box; it's just there to see how it could be done :)
#!/bin/bash
# Scan the files you need
for i in *jpg *mov; do
# Get the extension of the previous file
extension=${i##*.}
# Extract metadata from the file
model=$( exiftool -f -s3 -"Model" "${i}" )
make=$( exiftool -f -s3 -"Make" "${i}" )
datetime=$( exiftool -f -s3 -"DateTimeOriginal" "${i}" )
# If the datetime value is empty (returned '-' because of the '-f' option)
# then read another tag
if [ "${datetime}" = '-' ]; then
datetime=$( exiftool -f -s3 -"MediaCreateDate" "${i}" )
fi
# Construct the new filename from the metadata gathered above
newfilename="${datetime}-${make}-${model}.${extension}"
# Check if the destination filename already exists, and if not,
# create the file
if [ ! -e "${newfilename}" ]; then
echo mv "${i}" "${newfilename}"
else
echo "${i} would get renamed to ${newfilename} but that file already exists."
fi
done