Multiple but separate -if & tag writing ops

Started by mazeckenrode, October 13, 2020, 11:22:41 PM

Previous topic - Next topic

mazeckenrode

The developers of Directory Opus now seem to be gravitating towards discontinuation of their inclusion of a Unicode BOM in various plain string EXIF tags. That being the (possible future) case, I was hoping to develop an ExifTool command line to remove any and all BOMs from all existing tags in image files I've already written tags to. My reading of the instructions for -if leads me to believe that it isn't possible to test for AND rewrite multiple tags in files using -if, unless ALL of the tags being tested for exist, and this seems to be confirmed by the following test I performed:

exiftool -if "$EXIF:Software" "-EXIF:Software<${EXIF:Software;s/^(?:\xEF\xBB\xBF)?//}" -if "$EXIF:ImageDescription" "-EXIF:ImageDescription<${EXIF:ImageDescription;s/^(?:\xEF\xBB\xBF)?//}" .

Since EXIF:ImageDescription didn't exist in my target files, neither tag got rewritten.

My hoped-for command would be testing for and rewriting up to seven different EXIF tags in all applicable files, and it would be great if I didn't have to run ExifTool separately for each tag, but wanted to ask if maybe I'm missing anything, or any other tricks are available to streamline the operation.

StarGeek

I use a shortcut in my config file to group together my most commonly encountered tags and run an -api Filter command to check and correct them all at once.  In my case, it's to remove any leading/trailing spaces.  So here it is as an example

exiftool -P -overwrite_original -progress -echo "Trimming leading and trailing spaces" -if "$CommonStringTags ne $CommonStringTags#" -api "Filter=s/^\s+|\s+($)//g" -tagsfromfile @ -CommonStringTags

I've defined the CommonStringTags in the Image::ExifTool::UserDefined::Shortcuts second of my config file like this, allowing me to trim over thirty tags in one shot
CommonStringTags=>['Artist', 'AuthorsPosition', 'Caption-Abstract', 'CaptionWriter', 'City', 'Copyright', 'CopyrightNotice',
'Country', 'Country-PrimaryLocationName', 'Credit', 'Description', 'EditStatus', 'ImageDescription', 'Instructions',
'IPTC:Headline','Make', 'ObjectName', 'Province-State', 'rights', 'Source', 'SpecialInstructions', 'State', 'Title',
'TransmissionReference', 'URL', 'UserComment', 'WebStatement', 'Xmp:headline', 'XPAuthor', 'XPComment',
'XPKeywords', 'XPSubject', 'XPTitle','Comment'],


If any of these tags have a leading or trailing space, the all of them get re-written.  It's a shotgun approach, but it's still pretty quick when it encounters a positive result.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

mazeckenrode

Quote from: StarGeek on October 13, 2020, 11:37:48 PM
If any of these tags have a leading or trailing space, the all of them get re-written.

Thanks for the suggestion and example. I haven't tried tweaking it for my purposes yet, but my first thought was that it sounds like, as long as it found one existing tag being tested for, it would write all the tags, even if some of the rest of them didn't already exist, which isn't quite what I was aiming for. Before I start tweaking, does my assumption sound correct? Though even if so, that gives me an idea for a different strategy: Maybe I should just go ahead and let ExifTool write all the tags with one command line, even if they don't already exist (don't even bother testing for them), then have a second command line to delete any tags that exist only as empty strings, assuming that's possible (I think it is, but haven't tried my hand at it yet). It should beat having to run ExifTool once for each of seven tags.

StarGeek

Quote from: mazeckenrode on October 14, 2020, 03:07:27 PM
Thanks for the suggestion and example. I haven't tried tweaking it for my purposes yet, but my first thought was that it sounds like, as long as it found one existing tag being tested for, it would write all the tags, even if some of the rest of them didn't already exist

No, when copying like this, it won't create any tags in the shortcut group that didn't already exist.  Now if you do something like -CommonStringTags="write all tags", that would attempt to write the value to all the tags.

Quotethen have a second command line to delete any tags that exist only as empty strings, assuming that's possible (I think it is, but haven't tried my hand at it yet). It should beat having to run ExifTool once for each of seven tags.

If the tag exists, but contains an empty string, then you can use -TAG-= to remove the empty tags.  This can also be done with a shortcut tag e.g -CommonStringTags-=.  My first command is actually a part of a trim and clear args file.  The clear part is run after the trim because if, for example, a tag contains nothing by 10 blank spaces, then -TAG-= won't clear it, because it's not empty.  So I trim first and then it will get removed with the second pass.

You can even do something like -All:All-= which will removed any empty tags that can be removed, but I wouldn't recommend it.  It turns out it's very slow, as I assume it's check every possible tag.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

mazeckenrode

Quote from: StarGeek on October 14, 2020, 07:00:27 PM
No, when copying like this, it won't create any tags in the shortcut group that didn't already exist.

Excellent, and thanks again. Got this to work, seemingly as hoped:

[Within config file "MAZEShortcuts.config"]


MAZEStrings => ['EXIF:ImageDescription','EXIF:XPSubject','EXIF:XPComment','EXIF:XPTitle','EXIF:UserComment','EXIF:Make','EXIF:Model','EXIF:Software','EXIF:Artist','EXIF:Copyright','EXIF:LensMake','EXIF:LensModel','IPTC:ObjectName','IPTC:Caption-Abstract','XMP:Title','XMP:Description','IPTC:CopyrightNotice','XMP:LensMake','XMP:LensModel','XMP:LensSerialNumber']


[Command line]

exiftool -config MAZEShortcuts.config -overwrite_original -progress -echo "Removing BOM from string tags" -if "$MAZEStrings ne $MAZEStrings#" -api "Filter=s/^(?:\xEF\xBB\xBF)//g" -tagsfromfile @ -MAZEStrings .

If you don't mind, though, please explain this part of the command line: -if "$CommonStringTags ne $CommonStringTags#". Paraphrased in English, I'm fairly sure that means, "If the value of CommonStringTags is not equal to the print-conversion-disabled value of CommonStringTags", and is used to test for the existence of any of the indivudal tags represented by CommonStringTags. If a tag doesn't exist, the two values would be equal, rather than the comparison just fail? And the two values would never be equal otherwise? For example, in two post-op exported JSONs, one with print conversion and the other without, the following displayed values sure look the same to me:


"XMP:UserComment": "<comment>",
"XMP:UserComment": "<comment>",

StarGeek

Quote from: mazeckenrode on October 15, 2020, 05:39:45 PM
If you don't mind, though, please explain this part of the command line: -if "$CommonStringTags ne $CommonStringTags#". Paraphrased in English, I'm fairly sure that means, "If the value of CommonStringTags is not equal to the print-conversion-disabled value of CommonStringTags",

That is correct

Quoteand is used to test for the existence of any of the indivudal tags represented by CommonStringTags. If a tag doesn't exist, the two values would be equal, rather than the comparison just fail?

The API Filter call affects all tags.  So it would affect all tags in CommonStringTags, if possible.  But the hashtag gives the values of all the tags in their original form.  So in the case of MAZEStrings and your Filter to remove BOMs, it would compare the filtered tags to the non-filtered tags.  So if nothing was changed, then they would be equal.

If I recall correctly, when using a shortcut to compare, it concats all the tags.  It is also affected by the -sep option.  See this post for some tricky use of renaming with a shortcut and the -sep option.

Though I just realized that there are circumstances where it would never be equal because the base tag is PrintConv-ed.  For example, Orientation will never equal Orientation#.  Never really thought about it before and I guess I've been lucky that it's never bitten me in the butt.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

mazeckenrode

Thanks, StarGeek, for the explanation and further reading.