summaryrefslogtreecommitdiff
path: root/rename-pix-by-time-and-cam
blob: 9595d863d04e0177ae3837b42b3505f089584e1e (plain)
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
#!/usr/bin/env ruby

cams = { "SIGMA DP2 Merrill" => "dp2m", "FP2" => "fp2", "Canon EOS 1000D" => "1000d", "iPad Air" => "ipadair" }
cams.default = "default"

ids = []
while line = STDIN.gets # list of files
  a = line.split('.')
  a.pop
  ids << a.join('.')
end

used_prefixes = {} # prefix -> 0..N

ids.each do |i|
  exif = `exiftool #{i}.jpg 2>/dev/null`
  a = exif.split "\n"
  timestamp = nil
  cam = nil
  a.each { |j|
    if j.start_with? "Camera Model Name"
      cam = j
    elsif j.start_with? "Date/Time Original"
      timestamp = j
      next
    else
      next
    end
  }
  skip = false
  t = ""
  c = ""
  new_prefix = ""
  add = 0
  begin
    t = timestamp.split(':',2)[1].strip.gsub(/(:|\ )/, '-')
    c = cams[cam.split(':',2)[1].strip]
    new_prefix = "#{t}-#{c}"
    add = 1
  rescue
    puts "Can't find metadata for #{i}, skipping!"
    skip = true
  end
  if t.split('-').first.to_i < 2000
    puts "metadata unreasonable for #{i}, skipping!"
    skip = true
  end
  next if skip
  while used_prefixes.has_key? new_prefix
    new_prefix = "#{t}-#{add}-#{c}"
    add += 1
  end
  used_prefixes[new_prefix] = true

  Dir.glob("#{i}*").each { |f|
    ext = f.gsub(/^#{i}/,"")
    if File.exists? "#{new_prefix}#{ext}"
      puts "File exists: #{new_prefix}#{ext} (#{i})!"
      exit
    else
      `mv #{i}#{ext} #{new_prefix}#{ext}`
    end
  }
end