class MiniMediainfo::Media

Class for parsing output from mediainfo

Attributes

uri[R]

Public Class Methods

new(uri, options={}) click to toggle source
# File lib/mini_mediainfo/media.rb, line 11
def initialize(uri, options={})
  @uri = uri
  @introspection_data = {}
end

Public Instance Methods

introspect() click to toggle source
# File lib/mini_mediainfo/media.rb, line 16
def introspect()
  cmd = "mediainfo \"#{@uri}\" -f"
  key = ''
  lines = []
  keys = []
  entries = []

  Open3.popen2e(cmd) do |stdin, stdout_err, wait_thr|
    while line = stdout_err.gets
      lines << line
    end

    exit_status = wait_thr.value
    unless exit_status.success?
      abort "FAILED !!! #{cmd}"
    end
  end

  key = ''
  lines.each do |l|
    if l.index(':').to_i > 0
      media_attrs = [l.slice(0..l.index(':')-1), l.slice(l.index(':')+1..-1)].collect {|a| a.strip}
    else
      if l.strip.length > 0
        key = l.strip
        keys.push(key)
      end
    end

    if (key && key.length > 0) && (media_attrs && media_attrs.length == 2)
      # Only add the attribute if it does not exist already. When we parse
      # full output from mediainfo we are only interested in the first
      # entry that is unformatted and easier to work with
      unless entries.find {|i| i[0] == key && i[1] == media_attrs[0]}
        entries.push([key, media_attrs[0], media_attrs[1]])
      end
    end
  end

  keys.each do |k|
    per_key_entries = entries.select {|e| e[0] == k}
    per_key_hash = Hash[per_key_entries.each {|e| e.shift}]
    @introspection_data[k] = per_key_hash
  end
end
meta() click to toggle source
# File lib/mini_mediainfo/media.rb, line 62
def meta()
  return @introspection_data
end