ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: fierodan on April 25, 2019, 12:15:29 PM

Title: DateTimeOriginal from hex timestamp in filename
Post by: fierodan on April 25, 2019, 12:15:29 PM
I have several images with a filename like: CEN_MT_CT_ABC01_5bc930ab4e535.png

The file modify date is not accurate and there is no existing EXIF data, so the only thing I have to go on is the first 8 characters of the hex string in the filename.

I can run the following Linux command...

for i in *.png; do date '+%Y:%m:%d %T%:z' --date="$(date -d @$(printf "%d" 0x${i:16:8}))"; done

and get the image date and time...

2018:10:18 21:17:31-04:00

I've been trying to figure out how to extract the hex timestamp from the filename and use it to populate the EXIF dates.

I found a couple examples Phil posted on this forum...

exiftool "-testname<${filename;s/(\d{3})\..*//;$_=$self->InverseDateTime($_);DateFmt(qq(%Y%m%d_%H%M%S_$1))}.%e" -d %s DIR
exiftool "-datetimeoriginal<${filename;$_=substr($_,0,13)} 00:00" DIR

...and tried incorporating s and substr but could not come up with an exiftool command to automate this.

Of course using this works...

exiftool -alldates="2018:10:18 21:17:31-04:00" CEN_MT_CT_ABC01_5bc930ab4e535.png

...but I have to copy and paste the output from my loop above into a command for each file one at a time.

I think I am on the right track, I just can't figure out the syntax.
Title: Re: DateTimeOriginal from hex timestamp in filename
Post by: Phil Harvey on April 25, 2019, 12:38:56 PM
Try this:

exiftool '-datetimeoriginal<${filename;$_=/_([0-9a-f]{8})/ ? ConvertUnixTime(hex($1),1) : undef}' DIR

However, EXIF DateTimeOriginal doesn't support a time zone directly.  You can use an XMP tag instead if you want to store the time zone.  The command above will convert to the local system time zone.

This uses the undocumented ExifTool ConvertUnixTime() function, so there is basically no way for you to figure this out from the documentation.

- Phil
Title: Re: DateTimeOriginal from hex timestamp in filename
Post by: StarGeek on April 25, 2019, 12:48:49 PM
Here I am, trying to pound out a step by step explanation based upon the first example, and Phil's comes along with yet another drop dead simpler way to do things

Not only am I too slow, but I'm making an overly complex answer.

At least I was on the right track with hex($1)
:D
Title: Re: DateTimeOriginal from hex timestamp in filename
Post by: Phil Harvey on April 25, 2019, 12:51:58 PM
:)
Title: Re: DateTimeOriginal from hex timestamp in filename
Post by: fierodan on April 25, 2019, 02:40:03 PM
That command worked great; thank you.