diff options
author | Patrick Simianer <pks@pks.rocks> | 2020-08-02 09:54:57 +0200 |
---|---|---|
committer | Patrick Simianer <pks@pks.rocks> | 2020-08-02 09:54:57 +0200 |
commit | bfe124440f48e23dc5b3e73dac6ead1928d885d5 (patch) | |
tree | 4d17f85990dfe8a7998a710e39ad2cea58b05ec1 | |
parent | 064bbd3f42e6f0d098a3c0ff6ace74c82d056a6d (diff) |
rename-pix-by-time-and-cam: refactor
-rwxr-xr-x | rename-pix-by-time-and-cam | 121 |
1 files changed, 73 insertions, 48 deletions
diff --git a/rename-pix-by-time-and-cam b/rename-pix-by-time-and-cam index 0dee002..226ade8 100755 --- a/rename-pix-by-time-and-cam +++ b/rename-pix-by-time-and-cam @@ -1,13 +1,19 @@ #!/usr/bin/env ruby -cams = { "SIGMA DP2 Merrill" => "dp2m", "FP2" => "fp2", "Canon EOS 1000D" => "1000d", "iPad Air" => "ipadair", "iPhone 5" => "iphone5", "iPhone SE" => "iphonese", "IQ180" => "iq180" } -cams.default = "default" +require 'date' + +$possible_devices = { "SIGMA DP2 Merrill" => "dp2m", "FP2" => "fp2", "Canon EOS 1000D" => "1000d", "iPad Air" => "ipadair", "iPhone 5" => "iphone5", "iPhone SE" => "iphonese", "IQ180" => "iq180", "iPhone XR" => "iphonexr" } +$possible_devices.default = "unknown-device" file_ext = ARGV[0] if !file_ext file_ext = "jpg" end +if ARGV[1] + $possible_devices.default = ARGV[1] +end + ids = [] while line = STDIN.gets # list of files a = line.split('.') @@ -17,76 +23,95 @@ end used_prefixes = {} # prefix -> 0..N +def parse_timestamp s + d = s.split(':', 2)[1] + d.strip! + d.gsub!(/^(\d\d\d\d):(\d\d):(\d\d)/, "\\1-\\2-\\3") + return DateTime.parse d +end + +def determine_datetime dts, subsec=nil + dt = dts.min # get the actual original date + if subsec != nil and subsec.to_i > 0 # add sub-seconds if available + if dt.strftime("%L") == "000" + s = format_datetime(dt)+".#{subsec}" + dt = DateTime.parse s + end + end + return dt +end + +def format_datetime dt + if dt.strftime("%L") == "000" + return dt.strftime("%Y-%m-%dT%H:%M:%S") + else + return dt.strftime("%Y-%m-%dT%H:%M:%S.%L") + end +end + +def parse_device s + d = s.split(':', 2)[1] + d.strip! + return $possible_devices[d] +end + +def determine_device a + a.each { |s| + if s != $possible_devices.default + return s + end + } + return $possible_devices.default +end + ids.each do |i| - exif = `exiftool #{i}.#{file_ext} 2>/dev/null` + exif = `exiftool "#{i}.#{file_ext}" 2>/dev/null` a = exif.split "\n" - timestamp = nil - timestamp_bak = nil - cam = nil - cam_bak = nil + timestamps = [] + subsec_time = nil + devices = [] a.each { |j| - if j.start_with? "Camera Model Name" - cam = j - elsif j.start_with? "Model" - cam_bak = j - elsif j.start_with? "Date/Time Original" - timestamp = j - elsif j.start_with? "File Modification Date/Time" - if not timestamp_bak - timestamp_bak = j - end - elsif j.start_with? "Creation Date" - timestamp_bak = j - else - next + if j.start_with? "Camera Model Name" \ + or j.start_with? "Model" + devices << parse_device(j) + elsif j.start_with? "Date/Time Original" \ + or j.start_with? "Creation Date" \ + or j.start_with? "Create Date" \ + or j.start_with? "Media Create Date" \ + or j.start_with? "Track Create Date" \ + or j.start_with? "File Modification Date/Time" + timestamps << parse_timestamp(j) + elsif j.start_with? "Sub Sec Time Original" + subsec_time = j.split(':', 2)[1].strip end } skip = false - t = "" - c = "" new_prefix = "" add = 0 begin - if timestamp - t = timestamp.split(':',2)[1].strip.gsub(/(:|\ )/, '-') - end - if timestamp_bak - t_bak = timestamp_bak.split(':',2)[1].strip.gsub(/(:|\ )/, '-') - end - if cam - c = cams[cam.split(':',2)[1].strip] - elsif cam_bak - c = cams[cam_bak.split(':',2)[1].strip] - else - c = "unknown-device" - end - if t.split('-').first.to_i < 2000 - t = t_bak - if not t or t.split('-').first.to_i < 2000 - puts "metadata unreasonable for #{i}, skipping!" - skip = true - end - end - new_prefix = "#{t}-#{c}" + t = format_datetime(determine_datetime(timestamps, subsec_time)) + d = determine_device devices + new_prefix = "#{t}-#{d}" add = 1 rescue - puts "Can't find metadata for #{i}, skipping!" + puts "Cannot determine metadata for #{i}, skipping!" skip = true end next if skip + while used_prefixes.has_key? new_prefix - new_prefix = "#{t}-#{add}-#{c}" + new_prefix = "#{t}-#{add}-#{d}" add += 1 end used_prefixes[new_prefix] = true Dir.glob("#{i}*").each { |f| - ext = f.gsub(/^#{i}/,"") + ext = f.sub("#{i}","") if File.exists? "#{new_prefix}#{ext}" puts "File exists: #{new_prefix}#{ext} (#{i})!" exit else - `mv #{i}#{ext} #{new_prefix}#{ext}` + `mv \"#{i}#{ext}\" #{new_prefix}#{ext}` end } end |