たくさん時間のずれた写真の時計合わせ

DP1M0538 🏛️

先日ポタリングに行ったのですが,その時持っていったカメラの時計が初期化されてしまっていてオフセットが6年半以上ありました.
(予備バッテリーが死んでバッテリー充電中に情報が初期化されてしまう)

これを正確な撮影時間に変更します.いつもはexiv2で設定していたのですが,今回これではうまく行かなかったのでexiftimeコマンドで設定しました.

先ずカメラ内時計と実際の時計の差分を求めます.正確な時間を撮影(今回はNICTのJST Clockを撮影)した結果はこんな感じでした.

$ ls --full-time DP1M0608.JPG
-rw-r--r-- 1 mk mk 9043186 2012-01-02 10:27:01.000000000 +0900 DP1M0608.JPG

写真に写った時間 -> 2018-07-17 07:32:00

それぞれUNIX EPOCに変換して引き算をすると差分は206312699秒でした.

$ date +%s -d '2012-01-02 10:27:01'
1325467621
$ date +%s -d '2018-07-17 07:32:00'
1531780320
$ expr 1531780320 - 1325467621
206312699

いつも使っているexiv2コマンドにこの秒を指定して修正しようとしたら日付がおかしくなってしまいました.

$ cp -p ./DP1M0608.JPG /tmp/DP1M0608.JPG
$ exiv2 -a +206312699 /tmp/DP1M0608.JPG
$ exiftime /tmp/DP1M0608.JPG
exiftime: field count mismatch (DateTime)
exiftime: field count mismatch (DateTimeOriginal)
exiftime: field count mismatch (DateTimeDigitized)
exiftime: field count mismatch (ImageUniqueID)
/tmp/DP1M0608.JPG: no timestamps available
$ exif /tmp/DP1M0608.JPG|grep -i date
Date and Time       |25548:01:17 21:27:01
Date and Time       |2012:01:02 10:27:01
Date and Time (Origi|25548:01:17 21:27:01
Date and Time (Digit|25548:01:17 21:27:01

manを確認すると,秒だけの指定は駄目でちゃんと計算して指定しないといけないようです.そして日またぎ以上は指定できないような感じです.これまではせいぜい数秒から数分しかずれなかったので気づきませんでした…….

       -a time
              Time  adjustment  in  the  format  [-]HH[:MM[:SS]]. This option is only used with the 'adjust' action. Examples: 1 adds one
              hour, 1:01 adds one hour and one minute, -0:00:30 subtracts 30 seconds.
 

計算するのが面倒なので差分を秒で指定できるツールはないだろうかと探してみると,exiftags packageのexiftimeが使えそうです.

              If val is numeric, one of either y, m, w, d, H, M, or S must be used to specify which part of the date is to be adjusted.
 

実際に試してみます.

念の為ファイルを/tmpにコピーして先ずはこれで試します.

$ cp -p ./DP1M0608.JPG /tmp/DP1M0608.JPG
$ exiftime /tmp/DP1M0608.JPG
exiftime: field count mismatch (ImageUniqueID)
Image Created: 2012:01:02 10:27:01
Image Generated: 2012:01:02 10:27:01
Image Digitized: 2012:01:02 10:27:01

-vでオフセットを指定します.今回は+206312699S206312699秒進めます.

$ exiftime -v +206312699S /tmp/DP1M0608.JPG
exiftime: field count mismatch (ImageUniqueID)
Image Created: 2018:07:17 07:32:00
Image Generated: 2018:07:17 07:32:00
Image Digitized: 2018:07:17 07:32:00

大丈夫そうです.
-vだけでは実際は書き換わりません-wで実際にexifを書き換えます.

$ exiftime -v +206312699S -w /tmp/DP1M0608.JPG
exiftime: field count mismatch (ImageUniqueID)
adjust time created in /tmp/DP1M0608.JPG from
  2012:01:02 10:27:01 to 2018:07:17 07:32:00? (y/n [n]) y
Image Created: 2012:01:02 10:27:01 -> 2018:07:17 07:32:00
adjust time generated in /tmp/DP1M0608.JPG from
  2012:01:02 10:27:01 to 2018:07:17 07:32:00? (y/n [n]) y
Image Generated: 2012:01:02 10:27:01 -> 2018:07:17 07:32:00
adjust time digitized in /tmp/DP1M0608.JPG from
  2012:01:02 10:27:01 to 2018:07:17 07:32:00? (y/n [n]) y
Image Digitized: 2012:01:02 10:27:01 -> 2018:07:17 07:32:00
$ exiftime /tmp/DP1M0608.JPG
exiftime: field count mismatch (ImageUniqueID)
Image Created: 2018:07:17 07:32:00
Image Generated: 2018:07:17 07:32:00
Image Digitized: 2018:07:17 07:32:00

exiftoolコマンドでファイルのタイムスタンプも修正します.

$ exiftool "-FileModifyDate<DateTimeOriginal" /tmp/DP1M0608.JPG
Warning: Bad PrintIM size - /tmp/DP1M0608.JPG
    1 image files updated
$ ls --full-time /tmp/DP1M0608.JPG
-rw-r--r-- 1 mk mk 9043186 2018-07-17 07:32:00.000000000 +0900 /tmp/DP1M0608.JPG

OKそうです.

本番のファイルを一気に書き換えてみます.
確認メッセージが出ないようexiftime-fオプションを付けています.

$ exiftime -v +206312699S -w -f ./*.JPG
./DP1M0537.JPG:
exiftime: field count mismatch (ImageUniqueID)
Image Created: 2012:01:01 08:46:51 -> 2018:07:16 05:51:50
Image Generated: 2012:01:01 08:46:51 -> 2018:07:16 05:51:50
Image Digitized: 2012:01:01 08:46:51 -> 2018:07:16 05:51:50
 
./DP1M0538.JPG:
exiftime: field count mismatch (ImageUniqueID)
Image Created: 2012:01:01 08:55:16 -> 2018:07:16 06:00:15
Image Generated: 2012:01:01 08:55:16 -> 2018:07:16 06:00:15
Image Digitized: 2012:01:01 08:55:16 -> 2018:07:16 06:00:15
 
   :
$ exiftime -v +206312699S -w -f ./*.X3F
   :

更に,gpscorrelateでジオタグを埋め込みます.

$ gpscorrelate -g ./2018-07-16鹿屋ポタ.gpx -z +9 -n *.JPG
$ gpscorrelate -g ./2018-07-16鹿屋ポタ.gpx -z +9 *.JPG

最後にファイルのタイムスタンプを修正します.

$ exiftool "-FileModifyDate<DateTimeOriginal" ./*

うまく行ったようです :)

環境

$ dpkg-query -W exiv2 exiftags exif
exif    0.6.21-2
exiftags        1.01-6build1
exiv2   0.25-3.1ubuntu0.18.04.1
$ lsb_release -d
Description:    Ubuntu 18.04.1 LTS
$ uname -m
x86_64

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)