Skip to content

Instantly share code, notes, and snippets.

@jmuspratt
Last active August 24, 2023 14:42
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jmuspratt/3680d45b0c12f8b32093 to your computer and use it in GitHub Desktop.
Save jmuspratt/3680d45b0c12f8b32093 to your computer and use it in GitHub Desktop.
exiftool : rename files in current dir to YYYY-MM-DD-HH-MM-SS-1.ext
# Function for your .zshrc file
# Requirements: install the package manager Homebrew (http://brew.sh), then install exiftool (type brew install exiftool in Terminal.app).
# Renames image and MOV files according to EXIF capture date, using YYYY-MM-DD-HH-MM-SS.ext format.
# Files shot within the same second get copy number added (-1,-2, etc.).
# Video files require a different, so we run exiftool 3 times:
# 1. Exclude MOV files and rename the image files with <CreateDate>.
# 2. Target MOV files and rename them with MediaCreateDate (for iPhone videos).
# 3. Target MOV files and rename them with DateTimeOriginal (for Fuji camera videos).
# References:
# http://www.sno.phy.queensu.ca/~phil/exiftool/
# http://ninedegreesbelow.com/photography/exiftool-commands.html#rename
# http://www.polaine.com/2015/01/fixing-photo-and-video-file-metadata-with-exiftool/
# Note: --ext EXCLUDES files with the extension, while -ext INCLUDES files with that extension
mediaName() {
exiftool --ext MOV '-filename<CreateDate' -d %Y-%m-%d-%H-%M-%S%%-c.%%le -api QuickTimeUTC=1 .;
exiftool -ext MOV '-filename<MediaCreateDate' -d %Y-%m-%d-%H-%M-%S%%-c.%%le -api QuickTimeUTC=1 .;
exiftool -ext MOV '-filename<DateTimeOriginal' -d %Y-%m-%d-%H-%M-%S%%-c.%%le -api QuickTimeUTC=1 .
}
@parhuzamos
Copy link

parhuzamos commented Aug 18, 2023

NOTE: For timezone handling, the switch -api QuickTimeUTC=1 should be added to the exiftool command (3 times). YMMV!
See https://exiftool.org/forum/index.php?topic=10070.0

In my case images and videos were taken by a Galaxy S4 phone. Date/time information in those .mp4 files are stored in UTC, but for images: it's the local date/time that exiftool displays (and uses for rename). So, without the
-api QuickTimeUTC=1 switch, videos are not renamed properly: time is shifted 1 hour (my timezone is UTC+1).

A list of phones with image/video files that I've checked:

  • HTC Desire HD (todo)
  • Sony XPeria X: needs -api QuickTimeUTC=1
  • Samsung Galaxy S4: needs -api QuickTimeUTC=1
  • Samsung Galaxy S7 (todo)
  • Samsung Galaxy S21 (todo)

(I'll get back after checking the other phones in the list.)

@jmuspratt
Copy link
Author

jmuspratt commented Aug 22, 2023

@parhuzamos Great catch! Thanks so much! Updated and wrapped this as a function now called mediaName()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment