Script to fix broken EXIF metadata (Offset/TimeZone) in assets #27324
yann117
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
As you might know,
Immichis currently not using theTZenvironment variable in the.envfile anymore.This lead to the timeline to be inconsistent with regards to your images and videos.
In the meantime to get this fixed, and at wider concern, since this will not fix assets that are in a different timezone than your usual one (system default), I wrote a small script to preprocess assets.
It will inject the missing
OffsetEXIF tags (from the official EXIF specifications) that our cameras/smartphones should write, but which is mostly never the case.I thought I would share it, as everyone face this issue.
Backup your library before testing/using this script, to avoid any data loss !!
Use the dry-run mode (default) first, analyze the result, use the
-verboseparameter mode if something look strange.Execute on a sample file in write mode with the
-runflag, and check the result/changes with:> exiftool -a -time:all -G0:1:2 -s my_file.jpgTo download and raise
bugsthere:https://github.com/yann117/media-fix
Full details below.
media-fix
media-fixis a Perl-based utility designed to repair, synchronize, and standardize metadata (EXIF/XMP) and filenames for photo and video collections. It handles complex timezone offsets and restores missing metadata using filename patterns.The default operating mode is
dry-runwhich will only reports the planned actions, the changes are applied only when-runis enabled.media-fixwill scan all your files, recursively, and use the MIME type to process images and videos.Usage
Options
YYYYMMDD_HHMMSS.FileModifyDateand timezone.-tz='Europe/Lisbon').process.log.High-Level Flow
At a high level,
media-fixfollows this sequence:-skiptagwas requested.-tagis enabled, write corrected metadata when needed.-renameis enabled, compute and stage a safe target filename when needed.-touchis enabled, update the file modification timestamp when needed.-xmpis enabled, generate an XMP sidecar when needed.Step-by-Step Explanation
1. Read EXIF metadata first unless
-skiptagis setIf the user passes
-skiptag, the method deliberately avoids reading embedded timestamps and logs that choice. This is useful when the file metadata is known to be wrong and the filename should be treated as the source of truth.Otherwise, the method tries to extract a
TargetDatefrom embedded EXIF metadata:MediaCreateDate,MediaModifyDate,TrackCreateDate, andTrackModifyDate.The first valid tag is being used for the rest of processing.
2. Parsing the filename
After EXIF extraction, the filename will be parsed for completing the metadata, also trying to get a
TargetDateif it was not yet found.Valid patterns that can be parsed:
YYYYMMDD_HHMMSSYYYYMMDD_HHMMSS.xxx±00:00IMG-20251130-WA0003.jpgAnything found after a matching date will be kept as
remainderand used for the final name.Some cleanup is applied to the
remainder, like removing trailing~1, ..., _01, ..., _HDRIf you want to rename only with the
YYYYMMDD_HHMMSSpattern, use the-strictparameter.Note for WhatsApp: The name/date is completely wrong, not corresponding to the actual date of the asset, but when the file was sent.
The hours/minutes/seconds will be computed from the trailing digits after
WAas a number of seconds added to12:00:00, the goal being to have distinct filenames.3. Determine TimeZone / Offset
If the
Offsetcouldn't be read from either the EXIF metadata or the filename,media-fixdefaults to the system local time.Note that if the
GPSDateStampandGPSTimeStampEXIF tags are present (picture with GPS details), then theOffsetis computed properly.You can change this behavior:
a whole override, with the-tzparameter in the command line, use a valid TimeZone from this list (highest priority).one folder only, creating an empty file inside that directory, starting with.tz(e.g.,.tz_europe_lisbon).Note on Videos: The tool automatically converts UTC-stored video metadata (QuickTime/MP4 standard) to your target or overridden timezone.
4. Repair EXIF metadata when tagging is requested
If
-tagis enabled, the method decides whether metadata should be rewritten.-skiptagwas used, meaning the filename-derived target should overwrite embedded metadataThe exact write strategy depends on the media type:
DateTimeOriginal,CreateDate, andModifyDatefields written using theTargetDate, along with their matching subsecond and offset tags.QuickTime:CreateDate,QuickTime:ModifyDate,QuickTime:MediaCreateDate,QuickTime:MediaModifyDate,QuickTime:TrackCreateDateandQuickTime:TrackModifyDatefields written, using theTargetDateconverted to UTC.If tagging was not needed, the method records a no-op result instead of staging writes.
5. Compute a safe rename
If
-renameis enabled, it builds the desired filename from the resolved target date:YYYYMMDD_HHMMSSYYYYMMDD_HHMMSS.xxxto avoid name collisionsIf the filename is already correct, nothing is renamed.
6. Align the filesystem modification date
This keeps the file’s modification timestamp aligned with the chosen target date in system-local time. It updates
FileModifyDatewhen:This makes the filesystem timestamp part of the same normalization pipeline.
7. Create an XMP sidecar when requested
If
-xmpis enabled, the script may create an.xmpsidecar, but only in the case:.xmpfile does not already existThe sidecar contains
XMP-exif:DateTimeOriginalset to the resolved target timestamp.This is effectively a non-destructive fallback path: instead of rewriting the asset itself, the script can externalize the corrected timestamp into XMP for downstream tools such as
Immich.With this method you can fix your library without touching your assets. Note however that Immich is currently (v2.6.1) not handling XMP files efficiently.
You will need to generate all your XMP, then in Immich navigate to Administration > Job Queues > Sidecar Metadata > Discover.
This will make your XMP files visible for Immich and will trigger the Extract Metadata (unfortunately on ALL you assets).
Additionnal scenario
Ignoring Directories
To skip a directory during recursive processing, place an empty file named
.noprocessin that folder.Forcing Incorrect EXIF Metadata
By combining filename recognition with the
-skiptagparameter, you can force or reset existing but incorrect EXIF tags.Simply rename your image/video file first with the desired date pattern you want and then run the command with both
-tagand-skiptagto overwrite the internal metadata.Restoring broken assets library
This tool can also be used to restore the correct filenames from a damaged filesystem.
After recovering the files with tools like
testdisk/photorec, you can rename images/videos from their EXIF tags using the-renametag:Examples
Recursive on folder tree, single-line output:
Single file analysis with logging:
Requirements
Image::ExifToolFile::Find::RuleBeta Was this translation helpful? Give feedback.
All reactions