Sha256: 94efe2b23459c4a9c58fe194eb6f4582976e3d69c122295a441a6e7e873ed3d1

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

require 'exifr'
require 'xmp'

class ExifExtractor

  # Extracts the metadata of a file at a given path in the file system.
  def metadata_for_file(file_path)
    img = EXIFR::JPEG.new(file_path)

    xmpValues = Hash.new

    extract_xmp_meta_data(img, xmpValues)
    extract_exif_main_meta_data(img, xmpValues)       
    extract_gps_infos(img, xmpValues)

    xmpValues.delete_if { |k, v| v.nil? || v.to_s.empty?}
  rescue => error
    Hash.new
  end

  # Check if ExifExtractor handles the file extension.
  # Example: ExifExtractor.handles? ".jpg" => returns true
  def self.handles?(file_extension)
    [".jpg", ".jpeg", ".tiff"].include? file_extension
  end

  private
  def extract_xmp_meta_data(img, xmpValues)
    xmp = XMP.parse(img)
    xmp.namespaces.each do |namespace_name|
        namespace = xmp.send(namespace_name)
        namespace.attributes.each do |attr|
        begin
        xmpValues["#{namespace_name}.#{attr}"] = namespace.send(attr)#.inspect
        rescue
        end
      end
    end   
  end

  def extract_exif_main_meta_data(img, xmpValues)
    xmpValues["exif.model"] =  img.model
    xmpValues["exif.artist"] = img.artist
    xmpValues["exif.date_time"] = format_date(img.date_time)
    xmpValues["exif.date_time_original"] = format_date(img.date_time_original)
    xmpValues["exif.width"] = img.width
    xmpValues["exif.height"] = img.height
  end


  def extract_gps_infos(img, xmpValues)
    if img.gps
      xmpValues["exif.gps.latitude"] = img.gps.latitude
      xmpValues["exif.gps.longitude"] = img.gps.longitude
      xmpValues["exif.gps.altitude"] = img.gps.altitude
    end
  end

  def format_date(date)
    date.strftime("%FT%T+00:00") unless date.nil?
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
filentory-cli-0.3.1 lib/filentory/exifextractor.rb
filentory-cli-0.3.0 lib/filentory/exifextractor.rb