ExifTool Script Issue: Photo Renaming Rules Applied to Video XMP in Mixed Direc

Started by effgee, May 05, 2025, 06:21:44 AM

Previous topic - Next topic

effgee

Hello ExifTool Community,

Came up short searching for prior issues..

I'm working on a bash script that uses ExifTool to organize and rename photo and video files, along with their .xmp sidecar files, based on metadata. The script processes files recursively in a source directory and moves the renamed files to a destination directory.

The script has distinct renaming rules for photos (using tags like DateTimeOriginal, ImageSize, Model) and videos (using tags like FileModifyDate, QuickTime:CreateDate, ImageSize, camera make/model tags relevant to videos).

The problem I'm encountering is when running the script on a directory containing both photo and video files. The section of the script intended for renaming XMP files associated with photos is incorrectly processing and applying photo renaming rules to XMP files that belong to videos. This results in video XMPs being renamed using photo metadata tags (or lack thereof), leading to incorrect filenames.

Here's a simplified look at the problematic part of the script structure and the photo XMP renaming command:

# ... script setup ...

# --- Photo Processing Section ---

# Command to rename photo files (works correctly)
# exiftool ... $EXTList_Photos ... -r "$SRCDIR"

# *** Problematic Photo XMP Renaming Command ***
read -p "Starting photo xmp rename..."
exiftool -m -v5 -P -V -ext xmp -d '%Y.%m.%d-[%B.%d]-%Hh%Mm%Ss' \
    -tagsfromfile $SRCDIR/%f.%e \
    '-filename<${DateTimeOriginal}-[$ImageSize]%+3c.%e' \
    '-filename<${DateTimeOriginal}-[$ImageSize]${Model;$_=".[$_]"}%+3c.%e' \
    "-Directory<$DSTDIR"/'${DateTimeOriginal#;DateFmt("%Y.%m-[%B.%Y]")}' \
    -fileOrder -filename -r "$SRCDIR"
echo "We finished photo xmp rename section..."
read -p "Press Enter to continue..."

# --- Video Processing Section ---

# Command to rename video files (works correctly)
# exiftool ... $EXTList_Videos ... -r "$SRCDIR"

# Separate Video XMP renaming command (intended, but was commented out or problematic)
# This command uses video-specific tags like FileModifyDate, QuickTime:CreateDate, etc.
# exiftool ... -ext xmp ... -tagsfromfile @ ... -r "$SRCDIR"

# ... rest of script ...


When this script runs in a mixed directory, the exiftool command targeting photo XMPs (using -ext xmp and -tagsfromfile $SRCDIR/%f.%e or subsequent attempts with @) processes all .xmp files it finds, including those associated with videos. Since video files often lack DateTimeOriginal and photo-specific ImageSize or Model tags, the renaming fails or produces incomplete filenames for the video XMPs using the photo pattern.

Attempted several approaches to fix this:

Using -tagsfromfile @:  changed -tagsfromfile $SRCDIR/%f.%e to -tagsfromfile @ in the photo XMP command, intending for ExifTool to read tags from the associated main file (photo or video).

Result: The photo command still processed video XMPs, and the lack of photo-specific tags in the video files led to incorrect renaming using the photo pattern.
Filtering with -if '$FileExtension =~ /.../': Added an -if condition to the photo XMP command (-if '$FileExtension =~ /^(jpg|heic|...)$/i') and planned to add a similar one for videos, to only process XMPs if their associated file's extension matched the respective type.

Result: This resulted in a "Condition: Search pattern not terminated" error, suggesting a parsing issue with the regular expression in the -if condition.
Filtering with -if '$FileExtension eq ...': To avoid the regex parsing error,  changed the -if condition to use equality checks (-if '$FileExtension eq "jpg" or $FileExtension eq "heic" ...').

Result: This produced a "Condition: Use of uninitialized value $info{"FileExtension"}" error, indicating that the $FileExtension tag from the associated file was not reliably available when the condition was being evaluated for some XMPs (again, likely video XMPs being processed by the photo block).
Combined Renaming: Tried combining the main file and XMP renaming into single ExifTool calls for photos and videos, using -ext xmp alongside the main extensions and repeating the renaming tags with -tagsfromfile @ for the XMP.

Result: The photo command is still somehow affecting the video XMPs.
It seems the core challenge is reliably filtering which XMP files are processed by the "photo" rules and which by the "video" rules when they are in the same directory, based on the type of their associated main file, within the ExifTool command structure.

Could anyone suggest a robust method using ExifTool command options, perhaps involving -if with a different tag like MIMEType, or a different command structure, to ensure that photo XMP renaming rules only apply to XMPs associated with photos and video XMP rules only apply to XMPs associated with videos in mixed content directories?

Any insights or examples would be greatly appreciated!

Thank you.

Phil Harvey

Honestly, this post was too long for me to read the whole thing, but I think the solution is likely to run a different based on the photo or video files (eg. one command with -ext jpg and the other with -ext mp4 or whatever), and use the -srcfile option to process the associated XMP files.

eg) exiftool -ext mp4 -srcfile %d%f.xmp ... DIR

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

effgee

Hey Phil! Thanks for the help. Yes, that did seem to work however the xmp gets renamed to the video .mp4 or whatever. Researching how not to have that happen....

Always blows my mind on how flexible exiftool is.. like I cannot wrap my head around it due to that. :)

exiftool -v5 -P -api QuickTimeUTC=0 $EXTList_Videos -srcfile %d%f.xmp -r "$SRCDIR" -d '%Y.%m.%d-[%B.%d]-%Hh%Mm%Ss'  ... 


OK just adjusted the naming template to use .xmp instead of %e
    '-filename<${DateTimeOriginal}-[$ImageSize]%+3c.%e' \
    '-filename<${DateTimeOriginal}-[$ImageSize]${Model;$_=".[$_]"}%+3c.%e' \

    '-filename<${DateTimeOriginal}-[$ImageSize]%+3c.xmp' \
    '-filename<${DateTimeOriginal}-[$ImageSize]${Model;$_=".[$_]"}%+3c.xmp' \

Side question..

How am I supposed to know whether to use  -api QuickTimeUTC=0 or -api QuickTimeUTC=1. I even had some files where I had to use 1 on the xmp, but 0 on the image file. Very confusing. I just double check and swap it out when there is a time mismatch but its hard to do so on a lot of files.

Phil Harvey

The QuickTimeUTC option doesn't affect XMP metadata.

Basically, if your video recording device knows about your time zone then you should be using the QuickTimeUTC option.  I think most newer cameras do, but most older cameras do not.

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