summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2016-09-08 16:01:25 +0200
committerPatrick Simianer <p@simianer.de>2016-09-08 16:01:25 +0200
commitd7b350ee3795e280f2945f7e926df36d0a682239 (patch)
tree76f010f657edf2dd9b3a72e9485a34ecbc0ecdb9
parent90aa5cb03c321c671ad8a9624a09af5fc3ef7925 (diff)
rename-pix-by-time-and-cam
-rwxr-xr-xrename-pix-by-time-and-cam54
1 files changed, 54 insertions, 0 deletions
diff --git a/rename-pix-by-time-and-cam b/rename-pix-by-time-and-cam
new file mode 100755
index 0000000..dc4041f
--- /dev/null
+++ b/rename-pix-by-time-and-cam
@@ -0,0 +1,54 @@
+#!/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
+ids.uniq!
+
+used_prefixes = {} # prefix -> 0..N
+
+ids.each do |i|
+ exif = `exiv2 #{i}.jpg 2>/dev/null`
+ a = exif.split "\n"
+ timestamp = nil
+ cam = nil
+ a.each { |j|
+ if j.start_with? "Camera model"
+ cam = j
+ elsif j.start_with? "Image timestamp"
+ timestamp = j
+ next
+ else
+ next
+ end
+ }
+ begin
+ t = timestamp.split(':',2)[1].strip.gsub(/(:|\ )/, '-')
+ c = cams[cam.split(':',2)[1].strip]
+ new_prefix = "#{t}-#{c}"
+ add = 1
+ 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}, exiting!"
+ exit
+ end
+ `mv #{i}#{ext} #{new_prefix}#{ext}`
+ }
+ rescue
+ puts "Error @#{i}"
+ end
+end
+