-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfile-retouch-by-exif
executable file
·88 lines (71 loc) · 2.18 KB
/
file-retouch-by-exif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env perl
use v5.18;
use strict;
use warnings;
use File::Next;
use Getopt::Long;
use Digest::SHA1;
use File::Touch;
use Image::ExifTool qw(:Public);
use Time::Moment;
sub tm_from_exif {
my ($info, $tag, $offset) = @_;
my $tm;
my $dto = $info->{$tag};
return unless $dto && $dto =~ /\A [12][0-9]{3}:[0-9]{2}:[0-9]{2} \s [0-9]{2}:[0-9]{2}:[0-9]{2} ([\+\-][0-9]{2}:[0-9]{2})? \z/x;
my @exif_dto = $dto =~ m/([\+\-]|[0123456789]+)/g;
if ($exif_dto[6] && $exif_dto[7] && $exif_dto[8]) {
my $sign = $exif_dto[6] eq '-' ? -1 : 1;
my $hh = 0 + $exif_dto[7];
my $mm = 0 + $exif_dto[8];
$offset = ($sign eq '-' ? -1 : 1) * $hh * 60 + $mm;
}
$tm = Time::Moment->new(
year => $exif_dto[0],
month => $exif_dto[1],
day => $exif_dto[2],
hour => $exif_dto[3],
minute => $exif_dto[4],
second => $exif_dto[5],
# nanosecond => 0,
offset => $offset,
);
return $tm;
}
sub main {
my ($opts, $args) = @_;
my @input = grep { -f || -d } @{ $opts->{i} };
my $iter = File::Next::files(@input);
my %plan;
while (defined( my $file = $iter->() )) {
next unless $file =~ / \.(jpg|mpo|heic|m4v|mp4) \z/xis;
my $info = ImageInfo($file, 'DateTimeOriginal', 'CreateDate');
my $tm_exif = tm_from_exif($info, 'DateTimeOriginal', Time::Moment->now->offset)
// tm_from_exif($info, 'CreateDate', 0);
if (! $tm_exif) {
say "SKIP: $file\tLack of DateTime in exif (DateTimeOriginal / CreateDate)";
next;
}
my $mtime = (stat($file))[9];
my $tm_mtime = Time::Moment->from_epoch($mtime);
if ($tm_mtime != $tm_exif) {
say "$tm_exif\t$file";
if ($opts->{n}) {
say "DRY RUN: $file => $tm_exif";
} else {
File::Touch->new( mtime => $tm_exif->epoch )->touch($file);
}
} else {
say "SKIP: exif time == mtime == $tm_exif\t$file";
}
}
}
my %opts;
GetOptions(
\%opts,
'i=s@',
'd=s',
'n',
'ignore-existing'
);
main(\%opts, [@ARGV]);