Keyword Count

Started by msbc, October 28, 2015, 07:27:21 PM

Previous topic - Next topic

msbc

Is there a way to output the number of items present in a tag, in this case the keywords tag? I can work this out using scripting but would like a way to do it directly with an exiftool command.

As an example, the following command and output on OS X:
10:23 ~/Pictures $ exiftool -keywords MSC_2015-03-03_3299.jpg
Keywords                        : Austurland Region, Iceland, Stokksnes, Vestrahorn, beach, black sand, landscape, mountain, mountains, sand, shoreline, sunset

I'd like to get exiftool to output 12.
- Mark

Phil Harvey

Sure.  This is very sneaky but try this:

exiftool -p "${keywords;$_=(()=/, /g)+1}" FILE

This returns the number of ", " strings in the Keywords plus 1.

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

StarGeek

#2
Of course, you have to know your data.  A single "Robert Downey, Jr." or worse, if it's last name first format "Downey, Robert, Jr." and your output is way off.  Changing the separator with -sep is the better option in this case.

I felt like playing around with this and came up with a user defined tag for this.
    KeywordCount => {
        Require => 'Keywords',
        ValueConv => q{
            my @list = ref $val ? @$val : ($val);
            return scalar @list;
        },
    },


Edit:  Ooops, this fails when it comes to single items.  Ignore me!
And the final, very messy single line option. 
exiftool -p "${keywords;my $h =  $$self{VALUE}{Keywords}; $_= scalar @$h }" FILE


Phil, is there a better way to access a list type variable as an array in the advanced formatting?
* 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).

Phil Harvey

Quote from: StarGeek on October 29, 2015, 04:45:34 PM
Phil, is there a better way to access a list type variable as an array in the advanced formatting?

Accessing VALUE directly is not advisable.  Instead, GetValue() should be used:

exiftool -p "${keywords;$_=()=$self->GetValue('Keywords')}" FILE

Here I use the trick of assigning to a temporary array to force list context for the GetValue call.

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