-
Notifications
You must be signed in to change notification settings - Fork 0
/
exifdater.rb
115 lines (97 loc) · 2.75 KB
/
exifdater.rb
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env ruby
#
# Runs through a folder and prepends all photo filenames with date and time
#
# Author:: Soon Van <[email protected]>
# Copyright:: Copyright 2013 Soon Van
# License:: BSD 3-Clause License <http://opensource.org/licenses/BSD-3-Clause>
#
# Usage:
#
# $ exifdater.rb path/to/photos
require 'exifr/jpeg'
@found_count = 0
@rename_count = 0
@datemod_count = 0
@skip_count = 0
@error_count = 0
def cleanup_filenames_with_date_modified(photo_dir)
Dir.foreach(photo_dir) do |photo|
if is_valid_photo(photo)
remove_date_modified(photo, photo_dir)
end
end
end
def is_valid_photo(filename)
return File.extname(filename).downcase.match(/\.jpe?g/)
end
def remove_date_modified(photo, photo_dir)
photo_base = photo_dir + File::SEPARATOR
photo_file = photo_base + photo
photo_modified = EXIFR::JPEG.new(photo_file).date_time
photo_taken = EXIFR::JPEG.new(photo_file).date_time_original
if !photo_modified.nil? && photo_taken != photo_modified
datetime_modified = photo_modified.strftime("%Y%m%d_%H%M%S")
if File.fnmatch('*' + datetime_modified + '*', photo)
filename_without_date_modified = photo_file.sub(datetime_modified, '')
filename_without_date_modified.sub!('--', '-')
begin
File.rename(photo_file, filename_without_date_modified)
@datemod_count += 1
rescue
@error_count += 1
end
end
end
end
def rename_photo(photo, photo_dir)
photo_base = photo_dir + File::SEPARATOR
photo_file = photo_base + photo
photo_taken = EXIFR::JPEG.new(photo_file).date_time_original
unless photo_taken.nil?
photo_stamp = photo_taken.strftime("%Y%m%d_%H%M%S")
if File.fnmatch(photo_stamp + '*', photo)
@skip_count += 1
else
begin
File.rename(photo_file, photo_base + photo_stamp + '-' + photo)
@rename_count += 1
rescue
@error_count += 1
end
end
else
@error_count += 1
end
end
def exifdater(photo_dir)
if File.directory?(photo_dir)
Dir.foreach(photo_dir) do |photo|
if is_valid_photo(photo)
@found_count += 1
rename_photo(photo, photo_dir)
end
end
cleanup_filenames_with_date_modified(photo_dir)
end
end
if ARGV[0].nil?
puts "! No folder specified."
puts "Use the following format: \n\n"
puts " exifdater.rb path/to/photos"
exit
else
exifdater(ARGV[0])
puts "\nLet's go marking dates in photos..."
puts @found_count.to_s + " found"
puts @rename_count.to_s + " renamed"
if @skip_count > 0
puts @skip_count.to_s + " skipped, date already in filename"
end
if @datemod_count > 0
puts @datemod_count.to_s + " renamed to remove date_modified from filename"
end
if @error_count > 0
puts @error_count.to_s + " photos could not be renamed"
end
end