Class: Exiftool

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/exiftool.rb,
lib/exiftool/result.rb,
lib/exiftool/version.rb,
lib/exiftool/field_parser.rb

Defined Under Namespace

Classes: ExiftoolNotInstalled, FieldParser, NoDefaultResultWithMultiget, NoSuchFile, NotAFile, Result

Constant Summary collapse

VERSION =
Gem::Version.new('0.8.0')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filenames, exiftool_opts = '') ⇒ Exiftool

Returns a new instance of Exiftool.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/exiftool.rb', line 39

def initialize(filenames, exiftool_opts = '')
  @file2result = {}
  filenames = [filenames] if filenames.is_a?(String)
  unless filenames.empty?
    escaped_filenames = filenames.map do |f|
      Shellwords.escape(self.class.expand_path(f.to_s))
    end.join(' ')
    # I'd like to use -dateformat, but it doesn't support timezone offsets properly,
    # nor sub-second timestamps.
    cmd = "#{self.class.command} #{exiftool_opts} -j -coordFormat \"%.8f\" #{escaped_filenames} 2> /dev/null"
    json = `#{cmd}`.chomp
    raise ExiftoolNotInstalled if json == ''
    JSON.parse(json).each do |raw|
      result = Result.new(raw)
      @file2result[result.source_file] = result
    end
  end
end

Class Method Details

.commandObject



16
17
18
# File 'lib/exiftool.rb', line 16

def self.command
  @command ||= 'exiftool'
end

.command=(path_to_exiftool) ⇒ Object



12
13
14
# File 'lib/exiftool.rb', line 12

def self.command=(path_to_exiftool)
  @command = path_to_exiftool
end

.exiftool_installed?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/exiftool.rb', line 20

def self.exiftool_installed?
  exiftool_version > 0
end

.exiftool_versionObject

This is a string, not a float, to handle versions like “9.40” properly.



25
26
27
# File 'lib/exiftool.rb', line 25

def self.exiftool_version
  @exiftool_version ||= `#{command} -ver 2> /dev/null`.chomp
end

.expand_path(filename) ⇒ Object

Raises:



29
30
31
32
33
# File 'lib/exiftool.rb', line 29

def self.expand_path(filename)
  raise(NoSuchFile, filename) unless File.exist?(filename)
  raise(NotAFile, filename) unless File.file?(filename)
  File.expand_path(filename)
end

Instance Method Details

#errors?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/exiftool.rb', line 74

def errors?
  @file2result.values.any? { |ea| ea.errors? }
end

#files_with_resultsObject



70
71
72
# File 'lib/exiftool.rb', line 70

def files_with_results
  results.map { |ea| ea.source_file }
end

#result_for(filename) ⇒ Object



66
67
68
# File 'lib/exiftool.rb', line 66

def result_for(filename)
  @file2result[self.class.expand_path(filename)]
end

#results(include_results_with_errors = false) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/exiftool.rb', line 58

def results(include_results_with_errors = false)
  if include_results_with_errors
    @file2result.values
  else
    @file2result.values.select { |ea| !ea.errors? }
  end
end