简体   繁体   English

Bash脚本仅将exif元数据的日期(不是时间)添加到没有EXIF元数据的照片(属于它们的目录)

[英]Bash script to add exif metadata date only (not time) to photos without EXIF metadata from the directory they belong to

I am trying to create a script using jhead or exiftool to help me fix the metadata issues I got due to an Apple photo export. 我正在尝试使用jheadexiftool创建脚本,以帮助解决由于Apple照片导出而导致的元数据问题。 I am trying to move away from the apple ecosystem but Apple photos didn't just use EXIF to sort photos but also import dates etc. So when I export original photos with EXIF data, they end up in directories containing the date but don't all have exif data. 我试图脱离苹果生态系统,但苹果照片不仅使用EXIF对照片进行排序,而且还导入日期等。因此,当我导出带有EXIF数据的原始照片时,它们最终会出现在包含日期的目录中,但不会都有exif数据。

For example: 例如:

/8 February 2011/img - 6676.JPG

Is the only photo in this directory that doesn't have metadata. 是该目录中唯一没有元数据的照片。 I would like to add the date (8-2-2011) to the metadata. 我想将日期(8-2-2011)添加到元数据中。

Not very good with bash so if anyone can help, I would greatly appreciate. bash不太好,所以如果有人可以帮助,我将不胜感激。

Continuing from the comments, you can easily convert the directory name to a date using date -d in bash. 从注释继续,您可以使用bash中的date -d轻松地将目录名称转换为日期。 The trick is parsing the directory from the full filename (eg /8 February 2011/img - 6676.JPG ). 诀窍是从完整文件名解析目录(例如, /8 February 2011/img - 6676.JPG )。 Since it is an absolute path, you will have to use string indexes to chop the leading '/' regardless whether you use the dirname command or you simply parse it with bash parameter expansion (which is the simplest here). 由于它是绝对路径,因此无论使用dirname命令还是使用bash 参数扩展对其进行解析(这是最简单的方法),您都必须使用字符串索引来截断前导的'/'

Here is a short example showing how to handle the directory you have given. 这是一个简短的示例,显示了如何处理给定的目录。 You may need to modify the jhead commands so test it before you just turn any script loose on all of your photos. 您可能需要修改jhead命令,以便对其进行测试,然后再对所有照片松开任何脚本。

#!/bin/bash

f="/8 February 2011/img - 6676.JPG"    ## example filename "$f"

[ "${f:0:1}" = '/' ] && d="${f:1}"     ## check/trim leading '/' for dir "$d"
d="${d%%/*}"                           ## strip filename leaving dir for time in $d
dt="$(date -d "$d" +'%d-%m-%Y')"       ## get date from $d in your format
jhdt="$(date -d "$d" +'%Y:%m:%d')"     ## get date in format required by jhead

## output dates and commands for jhead
printf "file    : \"%s\"\n" "$f"
printf "has date: %s\n" "$dt"
printf "jhead -mkexif \"%s\"\n" "$f"
printf "jhead -ds %s \"%s\"\n" "$jhdt" "$f"

Example Use/Output 使用/输出示例

$ bash dirtodate.sh
file    : "/8 February 2011/img - 6676.JPG"
has date: 08-02-2011
jhead -mkexif "/8 February 2011/img - 6676.JPG"
jhead -ds 2011:02:08 "/8 February 2011/img - 6676.JPG"

Thank you for your help. 谢谢您的帮助。 Here is my "final" dirty script to fix my 25k photo collection exif files, generated by Apple photos and to export to Plex or Google photos: 这是我的“最终”肮脏脚本,用于修复由Apple照片生成并导出到Plex或Google照片的25k照片收藏exif文件:

#!/bin/bash
FILES=/share/Photos/Tom/*/*.jpg
DATE_IMPORT="2017:09:1*"
for f in $FILES
do
  date_exif=`jhead -q "$f" | sed -n -e 's/^.*date //p' | cut -d' ' -f 5`
  #echo $date_exif
  if [[ $date_exif =~ $DATE_IMPORT ]]
     then
        aplevent=`echo "$f" | cut -d '/' -f 5`
        date=`echo "$aplevent" | rev | cut  -d',' -f 1 | rev;`
        date_folder="$(date -d "$date" +'%Y:%m:%d')"
        echo "Date exif: "$date_exif
        echo "Date fix:"$date_folder
        echo "$f"
        jhead -mkexif "$f"
        jhead -ds"$date_folder" "$f"
        jhead -ft "$f"
  fi
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM