Exiftool:将元数据从文本文件批量写入JPEG

问题描述 投票:1回答:2

我想使用ExifTool批量写入以前保存在文本文件中的元数据。

说我有一个包含以下JPEG文件的目录:

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

然后创建文件metadata.txt,其中包含文件名,后跟冒号,然后将其分发给同事,该同事将用所需的元数据填充该文件(在本例中为逗号分隔的IPTC关键字)。文件完成后将如下所示:

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

我将如何将该文件提供给ExifTool,并确保将正确的关键字保存到正确的文件中?如果有帮助,我也愿意更改文件的结构,例如将其格式化为CSV,JSON或YAML。

shell metadata jpeg exif exiftool
2个回答
0
投票

我认为您想要这个:

#!/bin/bash

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

这里是list.txt

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

这是结果:

ExifTool Version Number         : 11.11
File Name                       : 001.jpg
Directory                       : .
File Size                       : 358 bytes
File Modification Date/Time     : 2020:02:25 12:38:55+00:00
File Access Date/Time           : 2020:02:25 12:40:20+00:00
File Inode Change Date/Time     : 2020:02:25 12:38:55+00:00
File Permissions                : rw-r--r--
File Type                       : JPEG
File Type Extension             : jpg
MIME Type                       : image/jpeg
JFIF Version                    : 1.01
Resolution Unit                 : None
X Resolution                    : 1
Y Resolution                    : 1
Current IPTC Digest             : 5c7e64198628d6fb4cdb92b20ac0524c
Keywords                        :  KeywordA, KeywordB, KeywordC     <--- HERE THEY ARE
Application Record Version      : 4
Image Width                     : 1
Image Height                    : 1
Encoding Process                : Baseline DCT, Huffman coding
Bits Per Sample                 : 8
Color Components                : 3
Y Cb Cr Sub Sampling            : YCbCr4:4:4 (1 1)
Image Size                      : 1x1
Megapixels                      : 0.000001

如果您担心前导空格,可以更改脚本以使用冒号和空格作为分隔符:

while IFS=': ' read file keywords ; do

0
投票

如果可以将格式更改为CSV文件,则exiftool可以使用-csv option直接读取它。

您将必须以这种方式重新格式化。第一行必须在文件名上方具有“ SourceFile”标头,在关键字上方具有“ Keywords”标头。如果文件名不包含文件的路径,则必须从与文件相同的目录中运行命令。整个关键字字符串都需要用引号引起来,这样就不会将它们视为单独的列。结果将如下所示:

-csv

此时,您的命令将是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"

[C0需要确保将关键字视为单独的关键字,而不是单个长关键字。

与脚本循环遍历文件内容并为每行运行一次exiftool相比,这具有优势。 Exiftool的最大性能损失在于其启动,并且在循环中运行它会非常慢,尤其是对于大量文件(请参阅exiftool -csv=/path/to/file.csv -sep ", " /path/to/files)。

有关从csv文件读取的更多详细信息,请参见-sep option。>

© www.soinside.com 2019 - 2024. All rights reserved.