Parent

Namespace

MiniExiftool

Simple OO access to the Exiftool command-line application.

Constants

VERSION

Attributes

errors[R]
filename[R]

Public Class Methods

all_tags() click to toggle source

Returns a set of all known tags of Exiftool.

# File lib/mini_exiftool.rb, line 292
def self.all_tags
  unless defined? @@all_tags
    @@all_tags = pstore_get :all_tags
  end
  @@all_tags
end
command() click to toggle source

Returns the command name of the called Exiftool application.

# File lib/mini_exiftool.rb, line 277
def self.command
  @@cmd
end
command=(cmd) click to toggle source

Setting the command name of the called Exiftool application.

# File lib/mini_exiftool.rb, line 282
def self.command= cmd
  @@cmd = cmd
end
encoding_opt(enc_type) click to toggle source
# File lib/mini_exiftool.rb, line 57
def self.encoding_opt enc_type
  (enc_type.to_s + '_encoding').to_sym
end
exiftool_version() click to toggle source

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
from_hash(hash) click to toggle source

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
from_json(json, opts={}) click to toggle source

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
from_yaml(yaml) click to toggle source

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
new(filename=nil, opts={}) click to toggle source

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
opts() click to toggle source

Returns the options hash.

# File lib/mini_exiftool.rb, line 287
def self.opts
  @@opts
end
opts_accessor(*attrs) click to toggle source
# 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
original_tag(tag) click to toggle source

Returns the original Exiftool name of the given tag

# File lib/mini_exiftool.rb, line 308
def self.original_tag tag
  unless defined? @@all_tags_map
    @@all_tags_map = pstore_get :all_tags_map
  end
  @@all_tags_map[tag]
end
unify(tag) click to toggle source
# File lib/mini_exiftool.rb, line 324
def self.unify tag
  tag.to_s.gsub(/[-_]/,'').downcase
end
writable_tags() click to toggle source

Returns a set of all possible writable tags of Exiftool.

# File lib/mini_exiftool.rb, line 300
def self.writable_tags
  unless defined? @@writable_tags
    @@writable_tags = pstore_get :writable_tags
  end
  @@writable_tags
end

Public Instance Methods

[](tag) click to toggle source

Returns the value of a tag.

# File lib/mini_exiftool.rb, line 153
def [] tag
  @changed_values[tag] || @values[tag]
end
[]=(tag, val) click to toggle source

Set the value of a tag.

# File lib/mini_exiftool.rb, line 158
def []= tag, val
  @changed_values[tag] = val
end
changed?(tag=false) click to toggle source

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
changed_tags() click to toggle source

Returns an array of all changed tags.

# File lib/mini_exiftool.rb, line 190
def changed_tags
  @changed_values.keys.map { |key| MiniExiftool.original_tag(key) }
end
load(filename) click to toggle source

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() click to toggle source

Reload the tags of an already read file.

# File lib/mini_exiftool.rb, line 148
def reload
  load @filename
end
revert(tag=nil) click to toggle source

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() click to toggle source

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
save!() click to toggle source
# 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
tags() click to toggle source

Returns an array of the tags (original tag names) of the read file.

# File lib/mini_exiftool.rb, line 185
def tags
  @values.keys.map { |key| MiniExiftool.original_tag(key) }
end
to_hash() click to toggle source

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
to_yaml() click to toggle source

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.