Rename files depending on difference between exif date and a given date

Started by Gainor, January 23, 2017, 06:02:53 PM

Previous topic - Next topic

Gainor

Hi all. I'm new to exiftool but overwhelmed by the possibilities. Now I tried to manage a specific and tricky task but failed - maybe one of you can help me here:

I want to rename a bunch of photos in a directory (showing my daughter) depending on her age, i.e., depending on the difference between the date the picture was taken (read from EXIF data) and a given (on the command line) birthday date to compare to. Since I have, well, thousands of pictures of my children, this would be enormously helpful.
Ideally, the date difference should follow a format I use: aYbMcD_%%f.%%e, where a is the difference in full years, b the remaining full months and c the days, all with leading zeroes.
An example: A photo shot on, say, April 15th, 2016 and a given birthday date of May 2nd, 2010 would then be renamed to 05Y11M13D_<original filename> - and I could see from the file name that my daughter was 5 years, 11 months, and 13 days old when the picture was taken.

Is this possible with exiftool?

Thank you for your help,
Gainor

StarGeek

* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

Phil Harvey

Hi Gainor,

That long thread that StarGeek mentioned was similar, but it is actually very tricky calculating the days because they change for each month.  Attached is a config file that does this.  For this you enter the birthday with the -userparam option, so the command would look like this:

exiftool -config age.config "-filename<${age}_%f.%e" -userparam birthday="2010:05:02" DIR

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Gainor

Whoa, this is great, thank you! :) Your script does all the magic!

I didn't see the old thread with a similar (and even in parts more complex) intention and now I'm delving into it in order to find out what other things exiftool can do. This is a really powerful tool. You rock! ;)

Fulco

I use -if conditions to print the age of persons NAME1, NAME2 and/or NAME3, but between the lines Exiftool gives this warning/informational message: "Ignored -config option (not first on command line)". I tried to suppress the warning/informational messages with the -q -q options, but it didn't work. How can I print the three lines without the warnings/informational messages?

MacBook-Air-van-Fulco:~ Fulco$ exiftool -config age.config -if '$Subject=~/NAME1/' -q -q -p 'NAME1: $age' -userparam birthday="2007:12:23" -execute -config age.config -if '$Subject=~/NAME2/' -q -q -p 'NAME2: $age' -userparam birthday="2009:04:13" -execute -config age.config -if '$Subject=~/NAME3/' -q -q -p 'NAME3: $age' -userparam birthday="2010:07:12" -common_args /Users/Fulco/Pictures/Test/20140918175032.jpg
NAME1: 06 Years, 08 Month(s) and 26 Day(s)
Ignored -config option (not first on command line)
NAME2: 05 Years, 05 Month(s) and 05 Day(s)
Ignored -config option (not first on command line)
NAME3: 04 Years, 02 Month(s) and 06 Day(s)


- Fulco

Phil Harvey

Hi Fulco,

The -config option shouldn't be repeated after each -execute

I will make this clear in the documentation.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Fulco

Yes! It works. Thanks!

MacBook-Air-van-Fulco:~ Fulco$ exiftool -config age.config -if '$Subject=~/NAME1/' -q -q -p 'NAME1: $age' -userparam birthday="2007:12:23" -execute -if '$Subject=~/NAME2/' -q -q -p 'NAME2: $age' -userparam birthday="2009:04:13" -execute age.config -if '$Subject=~/NAME3/' -q -q -p 'NAME3: $age' -userparam birthday="2010:07:12" -common_args /Users/Fulco/Pictures/Test/20140918175032.jpg
NAME1: 06 Years, 08 Month(s) and 26 Day(s)
NAME2: 05 Years, 05 Month(s) and 05 Day(s)
NAME3: 04 Years, 02 Month(s) and 06 Day(s)

Phil Harvey

I've been playing around with this a bit more, and have worked up a more general config file that may be more widely useful.  I'm thinking of including this in the next ExifTool release.  See the comments in the file for documentation and examples.

@Gainor:  This new config file should work for you with the following command:

exiftool -config age.config "-filename<${age}_%f.%e" -userparam birthday="2010Y05M02D" DIR

(ie. change the specified Birthday to use the same format as you want for the Age.)

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Gainor

Thank you very much, this works like a charm. I'm happy that you want to add this to the next exiftool release, making this solution available to many more users. :)

One more idea: Could you add some functionality that checks whether the file already has a date string like this, i.e., whether the (identical) date string to be added is already there? Currently, if I use it in the same directory twice, I have to re-rename the files by hand and delete the duplicate date string. This might be interesting if new files are added to a directory and already treated pictures can then easily be skipped by the script, renaming only the newcomers.

Phil Harvey

You do this to avoid adding Age twice to the filename:

exiftool -config age.config "-filename<${age}_%f.%e" -userparam birthday="2010Y05M02D" -if "$filename !~ /$age/" DIR

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Fulco

Very cool if you include this feature into the next relaease! Is it also possible to calculate how many years, months, days, hours and minutes ago the picture was taken, i.e. the time span between the DateTimeOriginal and the current time ("now")?

- Fulco

PS, I'm not sure where to post this feature request.

Phil Harvey

...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

StarGeek

You can sorta do it with the same config, just input the current day.   The one problem is that it comes out negative.  Fixable if copying a tag with ${age;s/^-//}

* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

Phil Harvey

Calculating the negative time difference turns out to be more complicated than just removing the "-" sign.  Each of the components must be negated because they were subtracted in the wrong order.  Also, the number of days in the start month must change to the month in which the photo was taken instead of the birthday month.

Attached is an updated config file that handles negative time differences properly.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Fulco

Thank you Phil! I used your new config file to print the age of person and age of the image file itself.

MacBook-Air-van-Fulco:~ Fulco$ exiftool -config /Users/Fulco/Downloads/age.config -q -q -p 'Age of photo       : $Age' -userparam birthday= -execute -q -q -p 'Age person on photo: $Age' -userparam birthday="2000:07:02 18:28" -age -common_args /Users/Fulco/Pictures/Test/20160702_182802.jpg
Age of photo       : 00:06:22 21:40:00
Age person on photo: 16:00:00 00:00


If I don't use the -q -q options in the command, Exiftool gives warning messages (see below). Is it something to worry about?

MacBook-Air-van-Fulco:~ Fulco$ exiftool -config /Users/Fulco/Downloads/age.config -p 'Age of photo       : $Age' -userparam birthday= -execute -p 'Age person on photo: $Age' -userparam birthday="2000:07:02 18:28" -age -common_args /Users/Fulco/Pictures/Test/20160702_182802.jpg
Age of photo       : 00:06:22 21:41:15
Warning: ValueConv Age: Use of uninitialized value within @diff in numeric lt (<) - /Users/Fulco/Pictures/Test/20160702_182802.jpg
Age person on photo: 16:00:00 00:00
Warning: ValueConv Age: Use of uninitialized value within @diff in numeric lt (<) - /Users/Fulco/Pictures/Test/20160702_182802.jpg/


- Fulco