2

I'd like to use ExifTool to batch-write metadata that have been previously saved in a text file.

Say I have a directory containing the following JPEG files:

001.jpg 002.jpg 003.jpg 004.jpg 005.jpg

I then create the file metadata.txt, which contains the file names followed by a colon, and I hand it out to a coworker, who will fill it with the needed metadata — in this case comma-separated IPTC keywords. The file would look like this after being finished:

001.jpg: Keyword, Keyword, Keyword 
002.jpg: Keyword, Keyword, Keyword
003.jpg: Keyword, Keyword, Keyword
004.jpg: Keyword, Keyword, Keyword
005.jpg: Keyword, Keyword, Keyword

How would I go about feeding this file to ExifTool and making sure that the right keywords get saved to the right file? I'm also open to changing the structure of the file if that helps, for example by formatting it as CSV, JSON or YAML.

zool
  • 808
  • 8
  • 20

2 Answers2

3

If you can change the format to a CSV file, then exiftool can directly read it with the -csv option.

You would have to reformat it in this way. The first row would have to have the header of "SourceFile" above the filenames and "Keywords" above the keywords. If the filenames don't include the path to the files, then command would have to be run from the same directory as the files. The whole keywords string need to be enclosed in quotes so they aren't read as a separate columns. The result would look like this:

SourceFile,Keywords
001.jpg,"KeywordA, KeywordB, KeywordC"
002.jpg,"KeywordD, KeywordE, KeywordF"
003.jpg,"KeywordG, KeywordH, KeywordI"
004.jpg,"KeywordJ, KeywordK, KeywordL"
005.jpg,"KeywordM, KeywordN, KeywordO"

At that point, your command would be
exiftool -csv=/path/to/file.csv -sep ", " /path/to/files

The -sep option is needed to make sure the keywords are treated as separate keywords rather than a single, long keyword.

This has an advantage over a script looping over the file contents and running exiftool once for each line. Exiftool's biggest performance hit is in its startup and running it in a loop will be very slow, especially on a large amount of files (see Common Mistake #3).

See ExifTool FAQ #26 for more details on reading from a csv file.

StarGeek
  • 4,948
  • 2
  • 19
  • 30
  • This works...but then it tries to scan images that are not in my CSV. What gives? The CSV has, say, 10 images. But the folder has 20. The terminal spits out an error about those remaining 10 files. Why is it even trying to read those? They're not in the CSV... I get a line like this for every file: No SourceFile '/filepath/filenam.tif' in imported CSV database – Patrick Hennessey Aug 07 '20 at 00:19
  • Exiftool doesn't use the CSV as the file list, it's only the source of the data to embed. If the filepath doesn't match what's in the CSV, then it won't find a match. For example, if the CSV has the absolute path to the file, you can't CD to that directory and run the command there. The full filepath needs to be the input. For a further clarification, your best bet is the [ExifTool Forums](https://exiftool.org/forum), where the author is quite responsive to questions. – StarGeek Aug 07 '20 at 01:47
1

I believe the answer by @StarGeek is superior to mine, but I will leave mine for completeness and reference of a more basic, Luddite approach :-)

I think you want this:

#!/bin/bash

while IFS=': ' read file keywords ; do
    exiftool -sep ", " -iptc:Keywords="$keywords" "$file"
done < list.txt

Here is the list.txt:

001.jpg: KeywordA, KeywordB, KeywordC 
002.jpg: KeywordD, KeywordE, KeywordF
003.jpg: KeywordG, KeywordH, KeywordI

And here is a result:

exiftool -b -keywords 002.jpg
KeywordD
KeywordE
KeywordF

Many thanks to StarGeek for his corrections and explanations.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • 1
    This command will not write the keywords properly. It will write "KeywordA, KeywordB, KeywordC" as a single keyword, not as separate keywords. The inclusion of the [`-sep` option](https://ExifTool.org/exiftool_pod.html#sep-STR--separator), e.g. `-sep ", "` will split the string properly. – StarGeek Feb 25 '20 at 15:40
  • @StarGeek Thank you for pointing that out. It doesn't appear to make any difference though. If I do `exiftool -sep "," -iptc:Keywords="A,B,C" 001.jpg` I get just the same result when I check it. – Mark Setchell Feb 25 '20 at 15:51
  • 1
    Add the `-sep` option with different characters when you view the results. For example, `exiftool -sep ## -keywords file.jpg` to see the difference. Or add the `-b` options, `exiftool -b -keywords file.jpg`. Both will show that you have a single keyword of "KeywordA, KeywordB, KeywordC" rather than separate keywords of "KeywordA", "KeywordB", "KeywordC". See [ExifTool FAQ #17](https://exiftool.org/faq.html#Q17) for details. – StarGeek Feb 25 '20 at 16:02