DateTimeOriginal from hex timestamp in filename

Started by fierodan, April 25, 2019, 12:15:29 PM

Previous topic - Next topic

fierodan

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.

Phil Harvey

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
...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

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
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

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

fierodan