Calculate difference between DateTimeOriginal and the date of the folder

Started by rparadinha, February 18, 2018, 01:57:08 PM

Previous topic - Next topic

rparadinha

Hi everyone,

I catalog all my photo/video files inside a folder named by date taken ([YYYY-MM-DD-description]).
I'm updating all my photos/videos doing these changes:

  • Rename my all files to this format: YearMonthDay-HourMinuteSecond-Miliseconds-OriginalName
  • Fix all -DateTimeOriginal -CreateDate -ModifyDate tags
  • Add gps tags if they are empty

I've created a bash script to help including a function to do some validations.
For example I check if the DateTimeOriginal is different from the date of the folder (Directory)
Example:

exiftool -m -a -r -if '$Directory=~/.*\[(\d{4})\-(\d{2})\-(\d{2})\].*/ and ${Directory;s/.*\[(\d{4})\-(\d{2})\-(\d{2})\].*/${1}${2}${3}/} ne ${DateTimeOriginal;s/(\d{4}):(\d{2}):(\d{2})\ (\d{2}):(\d{2}):(\d{2})/$1$2$3/}' -p 'FileName:             ${FileName}' -p 'DirectoryDate:        ${Directory;s/.*\[(\d{4})\-(\d{2})\-(\d{2})\].*/${1}${2}${3}/}' -p 'DateTimeOriginal:     ${DateTimeOriginal;s/(\d{4}):(\d{2}):(\d{2})\ (\d{2}):(\d{2}):(\d{2})/$1$2$3/}' -p '-------------------------------------------------------------' DIR


My problem is, sometimes when we take photos at night we get past midnight the days are different even though they are in the same folder as I intend to do.
What I intend to do now is check if the DateTimeOriginal is different from the date of the folder (Directory), and that difference is greater than 5 days for example.
Is there any way to do this?

Thanks in advance.

Phil Harvey

Answering this will require some work.  I'll get back when I have time to do this.

- 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 ($).

rparadinha


Phil Harvey

OK.  Something very similar was explained here.

The only difference is that you need to reformat the date/time from the directory name:

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        TimeDiff => {
            Require => {
                0 => 'Directory',
                1 => 'DateTimeOriginal',
            },
            ValueConv => q{
                return undef unless $val[0] =~ /(\d{4})-(\d{2})-(\d{2})/;
                my $secs = Image::ExifTool::GetUnixTime($val[1]) - Image::ExifTool::GetUnixTime("$1:$2:$3 00:00:00");
                return $secs / (24 * 60 * 60);
            },
            PrintConv => 'Image::ExifTool::ConvertTimeSpan($val)',
        },
    },
);
1; #end


Also I have converted the time difference to days.

With this config file named "my.config" in the current directory, your command will look something like this:

exiftool -config my.config -m -a -r -if 'defined $timediff and $timediff > 0 and $timediff < 5' -p 'FileName:             ${FileName}' -p 'DirectoryDate:        ${Directory;s/.*(\d{4})-(\d{2})-(\d{2}).*/$1$2$3/}' -p 'DateTimeOriginal:     ${DateTimeOriginal;s/(\d{4}):(\d{2}):(\d{2})\ (\d{2}):(\d{2}):(\d{2})/$1$2$3/}' -p '-------------------------------------------------------------' 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 ($).

rparadinha

Thanks Phill for the quick response.
I added the abs function to always have the positive value.

my $secs = abs(Image::ExifTool::GetUnixTime($val[1]) - Image::ExifTool::GetUnixTime("$1:$2:$3 00:00:00"));

I did a few tests and I don't get why the > and define don't work inside of -if.
Here they are some examples I did:

Note: I'm using the Mac OS.

1 - Without -if

$ exiftool -config my.config -m -a -r -p 'FileName:             ${FileName}' -p 'DirectoryDate:        ${Directory;s/.*(\d{4})-(\d{2})-(\d{2}).*/$1$2$3/}' -p 'DateTimeOriginal:     ${DateTimeOriginal;s/(\d{4}):(\d{2}):(\d{2})\ (\d{2}):(\d{2}):(\d{2})/$1$2$3/}' -p 'TimeDiff:    $timediff' -p '-------------------------------------------------------------' \[2005-12-31\]\ Passagem\ de\ ano\ 2006/20060101-025736-000-100_0785.mov
FileName:             20060101-025736-000-100_0785.mov
DirectoryDate:        20051231
DateTimeOriginal:     20060101
TimeDiff:    1.12333333333333 seconds


2 - Only with define $timediff it matched.

$ exiftool -config my.config -m -a -r -if 'defined $timediff' -p 'FileName:             ${FileName}' -p 'DirectoryDate:        ${Directory;s/.*(\d{4})-(\d{2})-(\d{2}).*/$1$2$3/}' -p 'DateTimeOriginal:     ${DateTimeOriginal;s/(\d{4}):(\d{2}):(\d{2})\ (\d{2}):(\d{2}):(\d{2})/$1$2$3/}' -p 'TimeDiff:    $timediff' -p '-------------------------------------------------------------' \[2005-12-31\]\ Passagem\ de\ ano\ 2006/20060101-025736-000-100_0785.mov
FileName:             20060101-025736-000-100_0785.mov
DirectoryDate:        20051231
DateTimeOriginal:     20060101
TimeDiff:    1.12333333333333 seconds
-------------------------------------------------------------


3 - Using > operator failed

$ exiftool -config my.config -m -a -r -if '$timediff > 1' -p 'FileName:             ${FileName}' -p 'DirectoryDate:        ${Directory;s/.*(\d{4})-(\d{2})-(\d{2}).*/$1$2$3/}' -p 'DateTimeOriginal:     ${DateTimeOriginal;s/(\d{4}):(\d{2}):(\d{2})\ (\d{2}):(\d{2}):(\d{2})/$1$2$3/}' -p 'TimeDiff:    $timediff' -p '-------------------------------------------------------------' \[2005-12-31\]\ Passagem\ de\ ano\ 2006/20060101-025736-000-100_0785.mov
    1 files failed condition


4 - Using gt it works

$ exiftool -config my.config -m -a -r -if '$timediff gt 1' -p 'FileName:             ${FileName}' -p 'DirectoryDate:        ${Directory;s/.*(\d{4})-(\d{2})-(\d{2}).*/$1$2$3/}' -p 'DateTimeOriginal:     ${DateTimeOriginal;s/(\d{4}):(\d{2}):(\d{2})\ (\d{2}):(\d{2}):(\d{2})/$1$2$3/}' -p 'TimeDiff:    $timediff' -p '-------------------------------------------------------------' \[2005-12-31\]\ Passagem\ de\ ano\ 2006/20060101-025736-000-100_0785.mov
FileName:             20060101-025736-000-100_0785.mov
DirectoryDate:        20051231
DateTimeOriginal:     20060101
TimeDiff:    1.12333333333333 seconds
-------------------------------------------------------------


5 - But using a combination of define and gt doesn't work

$ exiftool -config my.config -m -a -r -if 'define $timediff and $timediff gt 1' -p 'FileName:             ${FileName}' -p 'DirectoryDate:        ${Directory;s/.*(\d{4})-(\d{2})-(\d{2}).*/$1$2$3/}' -p 'DateTimeOriginal:     ${DateTimeOriginal;s/(\d{4}):(\d{2}):(\d{2})\ (\d{2}):(\d{2}):(\d{2})/$1$2$3/}' -p 'TimeDiff:    $timediff' -p '-------------------------------------------------------------' \[2005-12-31\]\ Passagem\ de\ ano\ 2006/20060101-025736-000-100_0785.mov
    1 files failed condition


Phil Harvey

Ah.  TimeDiff has "seconds" added to it, so it isn't just numerical.  My mistake.  You should use $timeDiff# (with trailing "#") in the -if statement to compare the numerical value using ">".

- 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 ($).

rparadinha

It works with #:

$ exiftool -config my.config -m -a -r -if '$timediff# > 1' -p 'FileName:             ${FileName}' -p 'DirectoryDate:        ${Directory;s/.*(\d{4})-(\d{2})-(\d{2}).*/$1$2$3/}' -p 'DateTimeOriginal:     ${DateTimeOriginal;s/(\d{4}):(\d{2}):(\d{2})\ (\d{2}):(\d{2}):(\d{2})/$1$2$3/}' -p 'TimeDiff:    $timediff' -p '-------------------------------------------------------------' \[2005-12-31\]\ Passagem\ de\ ano\ 2006/20060101-025736-000-100_0785.mov
FileName:             20060101-025736-000-100_0785.mov
DirectoryDate:        20051231
DateTimeOriginal:     20060101
TimeDiff:    1.12333333333333 seconds

But the combination with define doesn't work:

$ exiftool -config my.config -m -a -r -if 'define $timediff# and $timediff# > 1' -p 'FileName:             ${FileName}' -p 'DirectoryDate:        ${Directory;s/.*(\d{4})-(\d{2})-(\d{2}).*/$1$2$3/}' -p 'DateTimeOriginal:     ${DateTimeOriginal;s/(\d{4}):(\d{2}):(\d{2})\ (\d{2}):(\d{2}):(\d{2})/$1$2$3/}' -p 'TimeDiff:    $timediff' -p '-------------------------------------------------------------' \[2005-12-31\]\ Passagem\ de\ ano\ 2006/20060101-025736-000-100_0785.mov
    1 files failed condition

Do you know the reason?

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 ($).