Object
Simple OO access to the Exiftool command-line application.
Returns the command name of the called Exiftool application.
# File lib/mini_exiftool.rb, line 277 def self.command @@cmd end
Setting the command name of the called Exiftool application.
# File lib/mini_exiftool.rb, line 282 def self.command= cmd @@cmd = cmd end
# File lib/mini_exiftool.rb, line 57 def self.encoding_opt enc_type (enc_type.to_s + '_encoding').to_sym end
Returns the version of the Exiftool command-line application.
# File lib/mini_exiftool.rb, line 316 def self.exiftool_version output = `#{MiniExiftool.command} -ver 2>&1` unless $?.exitstatus == 0 raise MiniExiftool::Error.new("Command '#{MiniExiftool.command}' not found") end output.chomp! end
Create a MiniExiftool instance from a hash. Default value conversions will be applied if neccesary.
# File lib/mini_exiftool.rb, line 256 def self.from_hash hash instance = MiniExiftool.new instance.initialize_from_hash hash instance end
Create a MiniExiftool instance from JSON data. Default value conversions will be applied if neccesary.
# File lib/mini_exiftool.rb, line 264 def self.from_json json, opts={} instance = MiniExiftool.new nil, opts instance.initialize_from_json json instance end
Create a MiniExiftool instance from YAML data created with MiniExiftool#to_yaml
# File lib/mini_exiftool.rb, line 272 def self.from_yaml yaml MiniExiftool.from_hash YAML.load(yaml) end
opts support at the moment
:numerical for numerical values, default is false
:composite for including composite tags while loading, default is true
:ignore_minor_errors ignore minor errors (See -m-option of the exiftool command-line application, default is false)
:coord_format set format for GPS coordinates (See -c-option of the exiftool command-line application, default is nil that means exiftool standard)
:replace_invalid_chars replace string for invalid UTF-8 characters or false if no replacing should be done, default is false
:timestamps generating DateTime objects instead of Time objects if set to DateTime, default is Time
ATTENTION: Time objects are created using Time.local therefore they use your local timezone, DateTime objects instead are created without timezone!
:exif_encoding, :iptc_encoding, :xmp_encoding, :png_encoding, :id3_encoding, :pdf_encoding, :photoshop_encoding, :quicktime_encoding, :aiff_encoding, :mie_encoding, :vorbis_encoding to set this specific encoding (see -charset option of the exiftool command-line application, default is nil: no encoding specified)
# File lib/mini_exiftool.rb, line 91 def initialize filename=nil, opts={} @opts = @@opts.merge opts if @opts[:convert_encoding] warn 'Option :convert_encoding is not longer supported!' warn 'Please use the String#encod* methods.' end @values = TagHash.new @changed_values = TagHash.new @errors = TagHash.new load filename unless filename.nil? end
Returns the options hash.
# File lib/mini_exiftool.rb, line 287 def self.opts @@opts end
# File lib/mini_exiftool.rb, line 39 def self.opts_accessor *attrs attrs.each do |a| define_method a do @opts[a] end define_method "#{a}=" do |val| @opts[a] = val end end end
Returns the value of a tag.
# File lib/mini_exiftool.rb, line 153 def [] tag @changed_values[tag] || @values[tag] end
Set the value of a tag.
# File lib/mini_exiftool.rb, line 158 def []= tag, val @changed_values[tag] = val end
Returns true if any tag value is changed or if the value of a given tag is changed.
# File lib/mini_exiftool.rb, line 164 def changed? tag=false if tag @changed_values.include? tag else !@changed_values.empty? end end
Load the tags of filename.
# File lib/mini_exiftool.rb, line 119 def load filename MiniExiftool.setup unless filename && File.exist?(filename) raise MiniExiftool::Error.new("File '#{filename}' does not exist.") end if File.directory?(filename) raise MiniExiftool::Error.new("'#{filename}' is a directory.") end @filename = filename @values.clear @changed_values.clear params = '-j ' params << (@opts[:numerical] ? '-n ' : '') params << (@opts[:composite] ? '' : '-e ') params << (@opts[:coord_format] ? "-c \"#{@opts[:coord_format]}\"" : '') @@encoding_types.each do |enc_type| if enc_val = @opts[MiniExiftool.encoding_opt(enc_type)] params << "-charset #{enc_type}=#{enc_val} " end end if run(cmd_gen(params, @filename)) parse_output else raise MiniExiftool::Error.new(@error_text) end self end
Reload the tags of an already read file.
# File lib/mini_exiftool.rb, line 148 def reload load @filename end
Revert all changes or the change of a given tag.
# File lib/mini_exiftool.rb, line 173 def revert tag=nil if tag val = @changed_values.delete(tag) res = val != nil else res = @changed_values.size > 0 @changed_values.clear end res end
Save the changes to the file.
# File lib/mini_exiftool.rb, line 195 def save MiniExiftool.setup return false if @changed_values.empty? @errors.clear temp_file = Tempfile.new('mini_exiftool') temp_file.close temp_filename = temp_file.path FileUtils.cp filename.encode(@@fs_enc), temp_filename all_ok = true @changed_values.each do |tag, val| original_tag = MiniExiftool.original_tag(tag) arr_val = val.kind_of?(Array) ? val : [val] arr_val.map! {|e| convert_before_save(e)} params = '-q -P -overwrite_original ' params << (arr_val.detect {|x| x.kind_of?(Numeric)} ? '-n ' : '') params << (@opts[:ignore_minor_errors] ? '-m ' : '') arr_val.each do |v| params << %(-#{original_tag}=#{escape(v)} ) end result = run(cmd_gen(params, temp_filename)) unless result all_ok = false @errors[tag] = @error_text.gsub(/Nothing to do.\n\z/, '').chomp end end if all_ok FileUtils.cp temp_filename, filename.encode(@@fs_enc) reload end temp_file.delete all_ok end
# File lib/mini_exiftool.rb, line 228 def save! unless save err = [] @errors.each do |key, value| err << "(#{key}) #{value}" end raise MiniExiftool::Error.new("MiniExiftool couldn't save. The following errors occurred: #{err.empty? ? "None" : err.join(", ")}") end end
Returns a hash of the original loaded values of the MiniExiftool instance.
# File lib/mini_exiftool.rb, line 240 def to_hash result = {} @values.each do |k,v| result[MiniExiftool.original_tag(k)] = v end result end
Returns a YAML representation of the original loaded values of the MiniExiftool instance.
# File lib/mini_exiftool.rb, line 250 def to_yaml to_hash.to_yaml end
Generated with the Darkfish Rdoc Generator 2.