Skip to content

Instantly share code, notes, and snippets.

@apsun
Last active November 21, 2023 20:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apsun/7c31f3443ec3f960e8856c570141ab80 to your computer and use it in GitHub Desktop.
Save apsun/7c31f3443ec3f960e8856c570141ab80 to your computer and use it in GitHub Desktop.
Rename iOS photos/videos using file metadata
#!/bin/bash
#
# A note on timezone handling: this script uses the local timezone in which
# the photo/video was originally taken.
#
# "CreateDate" stores the local time on images, but UTC on videos.
# "CreationDate" stores the full timestamp with timezone, but is only
# available on videos.
shopt -s nullglob
# Define output file naming convention
# %%+c adds _1, _2, etc. suffix for filename conflicts
# %%e adds original file extension
# e.g: IMG_20131218_090410_1.JPG
FMT_IMG='IMG_%Y%m%d_%H%M%S%%+c.%%e'
FMT_VID='IMG_%Y%m%d_%H%M%S%%+c.%%e'
# For older versions of iOS which don't set CreationDate, you'll get
# "Warning: No writable tags set from <file>" on video files. If that
# happens, set this variable to an appropriate value like "Asia/Shanghai"
FORCE_TZ=''
for f in *.{JPG,HEIC}; do
exiftool -q -ee -d "${FMT_IMG}" '-filename<CreateDate' "$f"
done
for f in *.MOV; do
if [ -z "${FORCE_TZ}" ]; then
exiftool -q -ee -d "${FMT_VID}" '-filename<CreationDate' "$f"
else
TZ="${FORCE_TZ}" exiftool -q -api QuickTimeUTC -ee -d "${FMT_VID}" '-filename<CreateDate' "$f"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment