Author Topic: DateTimeOriginal from hex timestamp in filename  (Read 112 times)

fierodan

  • Newbie
  • *
  • Posts: 2
DateTimeOriginal from hex timestamp in filename
« 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...

Code: [Select]
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

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 14438
    • ExifTool Home Page
Re: DateTimeOriginal from hex timestamp in filename
« Reply #1 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
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

StarGeek

  • Global Moderator
  • ExifTool Freak
  • *****
  • Posts: 2372
Re: DateTimeOriginal from hex timestamp in filename
« Reply #2 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
Troubleshooting hints:
* When posting, include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).
* Double all percent signs (%) in a Windows batch file.
* If your GPS coords are negative, make sure and set the GpsLatitudeRef and GpsLongitudeRef tags correctly.

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 14438
    • ExifTool Home Page
Re: DateTimeOriginal from hex timestamp in filename
« Reply #3 on: April 25, 2019, 12:51:58 PM »
:)
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

fierodan

  • Newbie
  • *
  • Posts: 2
Re: DateTimeOriginal from hex timestamp in filename
« Reply #4 on: April 25, 2019, 02:40:03 PM »
That command worked great; thank you.