exiftools从字母顺序文件名顺序更改小时

时间:2016-09-12 16:05:23

标签: bash jpeg

我想更改所有jpeg文件的小时数,已按字母顺序排序。

我手动排序我的相册,然后按字母顺序重命名文件。但是,如果我在Google Photo中推送此文件,则会删除该订单。 所以,我想只是逐步改变时间来尊重字母顺序,即我的专辑的顺序。

我在Ubuntu工作。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

这是一个可以帮助您入门的小bash脚本。它使用jhead一次处理当前目录中的所有JPG / JPEG文件。它从文件中提取日期/时间,并检查它是否与上一个文件不同。如果是新的一天,则在0,即新的一天的午夜开始小时,分钟和秒。如果它与前一个文件的日期相同,则会增加前一个文件的秒数 - 溢出为几分钟和几小时。

这意味着每天的第一个文件将以午夜时间戳出来,然后下一个文件将是00:00:01,下一个文件将是00:00:02,依此类推。当然,你可以改变方案。

#!/bin/bash

# Don't barf if no files or if files named with JPG,jpg,JPEG,jpeg
shopt -s nullglob
shopt -s nocaseglob

# Process files in current name order
for f in *.jpg *jpeg; do
   echo Processing $f...
   # Get current YMD, HMS in format yyyy:mm:dd and hh:mm:ss
   read ymd hms < <( jhead "$f" | awk '/Date/{print $3,$4}')
   [ ${#ymd} -lt 10 ] && { echo $f no date found - skipping; continue; }
   [ ${#hms} -lt 8 ] &&  { echo $f no time found - skipping; continue; }
   # Check if date has changed
   if [ "$ymd" != "$lastymd" ]; then
      # New day, reset hours, mins, secs to zero
      hrs=0
      mins=0
      secs=0
   else
      ((secs=secs+1))
      [ $secs -eq 60 ] && { ((mins=mins+1)); secs=0; }
      [ $mins -eq 60 ] && { ((hrs=hrs+1)); mins=0; }
      [ $hrs -gt 23 ] && { echo ERROR: Too many photos today $ymd; exit 1; }
   fi
   newts=$(printf "%s-%02d:%02d:%02d" $ymd $hrs $mins $secs)
   echo File: $f, current time: $ymd,$hms new time: $newts
   #jhead -ts"$newts" "$f"
   lastymd=$ymd
done

目前,它什么都不做,但如果您希望它实际修改您的文件,请删除最后一行第三行#前面的jhead

请首先备份并运行临时目录中的几个文件的副本。

示例输出

File: IMG_4045.JPG, current time: 2015:06:21,08:49:34 new time: 2015:06:21-00:00:00
Processing IMG_4046.JPG...
File: IMG_4046.JPG, current time: 2015:06:21,08:49:36 new time: 2015:06:21-00:00:01
Processing IMG_4047.JPG...
File: IMG_4047.JPG, current time: 2015:06:21,08:49:37 new time: 2015:06:21-00:00:02
Processing IMG_4048.JPG...
File: IMG_4048.JPG, current time: 2015:06:21,08:49:39 new time: 2015:06:21-00:00:03
Processing IMG_4049.JPG...
File: IMG_4049.JPG, current time: 2015:06:21,08:49:41 new time: 2015:06:21-00:00:04
Processing IMG_4052.JPG...
File: IMG_4052.JPG, current time: 2015:06:21,15:13:43 new time: 2015:06:21-00:00:00
Processing IMG_4053.JPG...
File: IMG_4053.JPG, current time: 2015:06:21,15:13:45 new time: 2015:06:21-00:00:01
Processing IMG_4054.JPG...
File: IMG_4054.JPG, current time: 2015:06:21,15:22:51 new time: 2015:06:21-00:00:02

答案 1 :(得分:0)

代码非常好。我稍微改变一下来修复一些错误或添加功能。

# Don't barf if no files or if files named with JPG,jpg,JPEG,jpeg
shopt -s nullglob
shopt -s nocaseglob

hrs=0
mins=0
secs=0

# Process files in current name order
for f in *.jpg *.jpeg *.mp4 *.mpg *.mov *.3gp *.gif *.png *.tif; do
   echo Processing $f...
   # Get current YMD, HMS in format yyyy:mm:dd and hh:mm:ss
   read ymd hms < <( jhead "$f" | awk '/Date/{print $3,$4}')
   [ "${ymd}" = "" ] && { ymd = "$lastymd"; }
   [ ${#ymd} -lt 10 ] && { echo "$f no date found"; ymd="$lastymd"; }
   [ ${#hms} -lt 8 ] &&  { echo "$f no time found"; ymd="$lastymd"; }
   # Check if date has changed
   if [ "$ymd" != "$lastymd" ]; then
      # New day, reset hours, mins, secs to zero
      hrs=0
      mins=0
      secs=0
   else
      ((secs=secs+1))
      [ "$secs" -eq 60 ] && { ((mins=mins+1)); secs=0; }
      [ "$mins" -eq 60 ] && { ((hrs=hrs+1)); mins=0; }
      [ "$hrs" -gt 24 ] && { echo ERROR: Too many photos today $ymd; exit 1; }
   fi
   newts=$(printf "%s %02d:%02d:%02d" $ymd $hrs $mins $secs)
   echo File: $f, current time: $ymd,$hms new time: $newts
   exiftool -overwrite_original -m "-alldates=$newts" "$f"
   exiftool -overwrite_original -m '-DateTimeOriginal>FileModifyDate' "$f"
   lastymd=$ymd
done