Created
May 9, 2022 18:15
-
-
Save criess/1aa7061d908f3c50f96d64bf0277cbf7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# reads filenames from stdin and renames them according to contained | |
# exif data | |
# | |
require 'time' | |
require 'yaml' | |
require 'fileutils' | |
filenames = ARGF.read.strip.split("\n") || [] | |
renames = [] | |
filenames.each do |full| | |
dirname = File.dirname full | |
begin | |
create_date = Time.strptime(`exiftool -createDate "#{full}"`[34..255].strip, "%Y:%m:%d %H:%M:%S") | |
rescue | |
begin | |
# try to use modifydate .... | |
create_date = Time.strptime(`exiftool -modifyDate "#{full}"`[34..255].strip, "%Y:%m:%d %H:%M:%S") | |
rescue | |
# try to use trackCreateDate | |
create_date = Time.strptime(`exiftool -trackCreateDate "#{full}"`[34..255].strip, "%Y:%m:%d %H:%M:%S") | |
end | |
end | |
extension = full.split('.').last | |
newfile = create_date.strftime("%Y-%m-%d-%H%M%S") + "_" + `sha224sum "#{full}"`[0..5].strip + "." + extension | |
renames << [full, File.join(dirname, newfile)] if full != File.join(dirname, newfile) | |
end | |
renames.each do |from, to| | |
puts "rename '#{from}' to '#{to}'" | |
FileUtils.mv(from, to) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment