Class: MediaInformationGatherer

Inherits:
Object
  • Object
show all
Defined in:
lib/mig.rb,
lib/mig/http.rb,
lib/mig/modules/common.rb,
lib/mig/modules/ffmpeg.rb,
lib/mig/modules/exiftool.rb,
lib/mig/modules/mediainfo.rb,
lib/mig/modules/media_type.rb

Defined Under Namespace

Classes: Common, ExifTool, FFMPEG, HTTP, MediaType, Mediainfo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_options = { }) ⇒ MediaInformationGatherer

Returns a new instance of MediaInformationGatherer.

Parameters:

  • _options (Hash) (defaults to: { })
  • options (Hash)

    a customizable set of options



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



35
36
37
# File 'lib/mig.rb', line 35

def log
  @log
end

#optionsObject (readonly)

Returns the value of attribute options.



35
36
37
# File 'lib/mig.rb', line 35

def options
  @options
end

Instance Method Details

#get_media_type_using_exiftoolObject

run_modules



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/mig.rb', line 133

def get_media_type_using_exiftool
  exiftool_md = [: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_filemagicObject



146
147
148
149
150
151
152
# File 'lib/mig.rb', line 146

def get_media_type_using_filemagic
  filemagic_md = [:filemagic]
  return unless filemagic_md.is_a?(Hash)
  return unless filemagic_md[:type]

  filemagic_md
end

#media_typeObject

initialize



66
# File 'lib/mig.rb', line 66

def media_type; @media_type ||= { } end

#metadata_sourcesObject



67
# File 'lib/mig.rb', line 67

def ;  ||= { } end

#run(file_path, options = { }) ⇒ Object

Parameters:

  • file_path (String)

    The path to the file to gather information about

Raises:

  • (Errno::ENOENT)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mig.rb', line 70

def run(file_path, options = { })
  @media_type = { }
   = { }

  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}"}
   = run_modules(file_path, options)
  log.debug { "Metadata gathering completed. Took: #{Time.now - gathering_start} seconds" }

  
end

#run_modules(file_path, options = { }) ⇒ Hash

Parameters:

  • file_path (String)

    The path of the file to gather information about

Returns:

  • (Hash)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# 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 [: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 [: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 [: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 [: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 [:exiftool] = @exiftool.run(file_path) rescue { :error => { :message => $!.message, :backtrace => $!.backtrace } }
    log.debug { "ExifTool took #{Time.now - start}" }
  end

  set_media_type
  [:media_type] = media_type

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

  
end

#set_media_typeObject



154
155
156
# File 'lib/mig.rb', line 154

def set_media_type
  @media_type = get_media_type_using_filemagic || get_media_type_using_exiftool
end