mrzool
mrzool

Reputation: 858

Exiftool: batch-write metadata to JPEGs from text file

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.

Upvotes: 2

Views: 3411

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 207485

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.

Upvotes: 1

StarGeek
StarGeek

Reputation: 5771

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.

Upvotes: 3

Related Questions