class MediaInformationGatherer

Constants

TO_HASH_METHODS

Attributes

log[R]
options[R]

Public Class Methods

new(_options = { }) click to toggle source

@param [Hash] _options @option options [String] :exiftool_cmd_path @option options [String] :ffmpeg_cmd_path @option options [String] :mediainfo_cmd_path

# File lib/mig.rb, line 41
def initialize(_options = { })
  @options = { }
  @options.merge!(_options)

  @log = options[:logger] || $log || Logger.new(STDOUT)
  log.debug { "#{self.class.name} - Options loaded. #{options}" }

  options[:logger] ||= log

  params = options.dup

  @exiftool = ExifTool.new( params )
  @ffmpeg = FFMPEG.new( params )
  @mediainfo = Mediainfo.new( params )

  @media_typer = MediaType.new

  @default_run_file_stat = options.fetch(:run_file_stat, true)
  @default_run_file_magic = options.fetch(:run_filemagic, true)
  @default_run_mediainfo = options.fetch(:run_mediainfo, true)
  @default_run_ffmpeg = options.fetch(:run_ffmpeg, true)
  @default_run_exiftool = options.fetch(:run_exiftool, true)
  @default_run_common = options.fetch(:run_common, true)
end

Public Instance Methods

get_media_type_using_exiftool() click to toggle source
# File lib/mig.rb, line 133
def get_media_type_using_exiftool
  exiftool_md = metadata_sources[:exiftool]
  return unless exiftool_md.is_a?(Hash)

  mime_type = exiftool_md['MIMEType']
  return unless mime_type.is_a?(String)

  type, sub_type = mime_type.split('/')
  return unless type

  { :type => type, :subtype => sub_type }
end
get_media_type_using_filemagic() click to toggle source
# File lib/mig.rb, line 146
def get_media_type_using_filemagic
  filemagic_md = metadata_sources[:filemagic]
  return unless filemagic_md.is_a?(Hash)
  return unless filemagic_md[:type]

  filemagic_md
end
media_type() click to toggle source
# File lib/mig.rb, line 66
def media_type; @media_type ||= { } end
metadata_sources() click to toggle source
# File lib/mig.rb, line 67
def metadata_sources; @metadata_sources ||= { } end
run(file_path, options = { }) click to toggle source

@param [String] file_path The path to the file to gather information about

# File lib/mig.rb, line 70
def run(file_path, options = { })
  @media_type = { }
  @metadata_sources = { }

  raise Errno::ENOENT, "File Not Found. File Path: '#{file_path}'" unless File.exist?(file_path)


  gathering_start = Time.now
  log.debug { "Gathering metadata for file: #{file_path}"}
  @metadata_sources = run_modules(file_path, options)
  log.debug { "Metadata gathering completed. Took: #{Time.now - gathering_start} seconds" }

  metadata_sources
end
run_modules(file_path, options = { }) click to toggle source

@param [String] file_path The path of the file to gather information about @return [Hash]

# File lib/mig.rb, line 87
def run_modules(file_path, options = { })
  run_file_stat = options.fetch(:run_file_stat, @default_run_file_stat)
  run_filemagic = options.fetch(:run_filemagic, @default_run_filemagic)
  run_mediainfo = options.fetch(:run_mediainfo, @default_run_mediainfo)
  run_ffmpeg = options.fetch(:run_ffmpeg, @default_run_ffmpeg)
  run_exiftool = options.fetch(:run_exiftool, @default_run_exiftool)
  run_common = options.fetch(:run_common, @default_run_common)

  if run_file_stat
    log.debug { 'Running File Stat.' }
    start = Time.now and metadata_sources[:stat] = File.stat(file_path).to_hash rescue { :error => { :message => $!.message, :backtrace => $!.backtrace } }
    log.debug { "File Stat took #{Time.now - start}" }
  end

  if run_filemagic
    log.debug { 'Running Filemagic.' }
    start = Time.now and metadata_sources[:filemagic] = @media_typer.run(file_path, options) rescue { :error => { :message => $!.message, :backtrace => $!.backtrace } }
    log.debug { "Filemagic took #{Time.now - start}" }
  end

  if run_mediainfo
    log.debug { 'Running MediaInfo.' }
    start = Time.now and metadata_sources[:mediainfo] = @mediainfo.run(file_path, options) rescue { :error => { :message => $!.message, :backtrace => $!.backtrace } }
    log.debug { "MediaInfo took #{Time.now - start}" }
  end

  if run_ffmpeg
    log.debug { 'Running FFMPEG.' }
    start = Time.now and metadata_sources[:ffmpeg] = @ffmpeg.run(file_path, options) rescue { :error => { :message => $!.message, :backtrace => $!.backtrace } }
    log.debug { "FFMpeg took #{Time.now - start}" }
  end

  if run_exiftool
    log.debug { 'Running ExifTool.' }
    start = Time.now and metadata_sources[:exiftool] = @exiftool.run(file_path) rescue { :error => { :message => $!.message, :backtrace => $!.backtrace } }
    log.debug { "ExifTool took #{Time.now - start}" }
  end

  set_media_type
  metadata_sources[:media_type] = media_type

  metadata_sources[:common] = Common.common_variables(metadata_sources) if run_common

  metadata_sources
end
set_media_type() click to toggle source
# File lib/mig.rb, line 154
def set_media_type
  @media_type = get_media_type_using_filemagic || get_media_type_using_exiftool
end
to_hash() click to toggle source
# File lib/mig.rb, line 20
def to_hash
  if defined?(__callee__)
    return (self.methods - Object.methods - [__callee__]).each_with_object({}) { |meth, acc| acc[meth] = self.send(meth) if self.method(meth).arity == 0 }
  else
    #(self.methods - Object.methods).each({}) { |meth, acc| acc[meth] = self.send(meth) if self.method(meth).arity == 0 }
    hash_out = { }
    TO_HASH_METHODS.each do |meth|
      hash_out[meth] = self.send(meth) if self.methods.include?(meth.to_s) and self.method(meth).arity == 0
    end
    return hash_out
  end
end