-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexif-offset.pl
More file actions
executable file
·74 lines (61 loc) · 2.05 KB
/
exif-offset.pl
File metadata and controls
executable file
·74 lines (61 loc) · 2.05 KB
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
#!/usr/bin/env perl
# Notes
use Image::ExifTool qw(:Public);
use Date::Manip;
# Usage:
# Increment/decrement dates by X years, months, days, hours, minutes, seconds
# Intended to be used on images containing EXIF data from digital cameras
# Original files are saved with *.bak extension
sub usage {
print STDERR
"Usage:\n\n $0 [+-][0-9]+[y|m|d|h|min|s] <files ...>\n\n",
;
exit;
}
# How much to shift the date/time by, e.g. :
# Years (Y), Months (M), Days (D), Hours (H), Minutes (min), Seconds (S)
my $offset = shift @ARGV;
# The name of the field containing the date.
# Format is e.g.: "2006:12:22 23:50:49"
my $tag = "DateTimeOriginal";
# Temp. var.
my $success;
usage() unless @ARGV;
# Shift date of each file by same offset
for my $file (@ARGV) {
# Get hash of meta information tag names/values from an image
my $info = ImageInfo($file);
# Create a new Image::ExifTool object
my $exifTool = new Image::ExifTool;
# Extract meta information from an image
$success = $exifTool->ExtractInfo($file);
unless ($success) {
print STDERR "Couldn't read EXIF data in file: $file\n";
next;
}
# Get the value of a specified tag
my $datestr = $exifTool->GetValue($tag);
print STDERR "old:$datestr:\n";
# Remove ':' and ' ' in order to be able to parse date
$datestr =~ s/[: ]//g;
# Shift date by given offset
my $ndate = DateCalc($datestr, $offset);
# Skip if no (valid) offset given
next unless $ndate;
# Output date into EXIF-compatible format
my $ndatestr = UnixDate($ndate, "%Y:%m:%d %H:%M:%S");
print STDERR "new:$ndatestr:\n";
# Set a new value for a tag
$success = $exifTool->SetNewValue($tag, $ndatestr);
unless ($success) {
print STDERR "Failed to set $tag to $ndatestr on $file\n";
next;
}
# Write new meta information to a file
rename $file, "${file}.bak";
$success = $exifTool->WriteInfo("${file}.bak", $file);
unless ($success) {
print STDERR "Failed to write $file\n";
next;
}
} # while