class Object

Public Instance Methods

mediainfo_run!(filename, mediainfo_object) click to toggle source

we use the mediainfo command-line interface to get the XML results it returns the raw XML data

# File lib/mediainfo-simple/parser.rb, line 49
def mediainfo_run! filename, mediainfo_object
  command = "#{mediainfo_path_verified} '#{filename}' --Output=XML"
  raw_xml_response = `#{command} 2>&1`
  unless $? == 0
    raise RuntimeError, "Execution of `#{command}` failed: #{raw_xml_response.inspect}"
  end
  parse! raw_xml_response, mediainfo_object
end
parse!(raw_xml_response, mediainfo_object) click to toggle source

we parse the raw XML data (with ReXML), and we fill the MediaInfo object attributes

# File lib/mediainfo-simple/parser.rb, line 60
def parse! raw_xml_response, mediainfo_object
  # puts "#{raw_xml_response}"
  REXML::Document.new(raw_xml_response).elements.each("/Mediainfo/File/track") { |track|
    # we create a "Stream" object, depending on the Stream type
    stream = StreamFactory.create track.attributes['type']

    # we get each tag about the stream
    track.children.select { |n| n.is_a? REXML::Element }.each do |c|
      # we convert the tag name to a ruby-attribute-compatible name
      tag_name = c.name.strip  # remove whitespaces at the beginning and the end
      tag_name = tag_name.gsub(/ +/, "_")  # we replace spaces by '_'
      # we replace characters forbidden in Ruby method names by '_':
      tag_name = tag_name.gsub(/[\(\)\{\}\[\]\*\/\\,;\.:\+=\-\^\$\!\?\|@#\&"'`]+/, '_')
      tag_name = tag_name.gsub(/^_+/, "")  # remove '_' at the beginning
      tag_name = tag_name.gsub(/_+$/, "")  # remove '_' at the end
      tag_name = tag_name.gsub(/_+/, "_")  # we replace several '_' following by a single one
      tag_name = tag_name.downcase

      # if there is an attribute in the Stream class,
      # that has the same name as the tag name, we set it with the tag content
      if stream.class.method_defined? tag_name + "="
        # we call the method which name is the content of the string tag_name
        stream.send tag_name + "=", c.text.strip
      else
        # to print the tag ignored, in case we want to support them
        # puts "#{stream.class}: tag ignored: #{tag_name}, #{c.text.strip}"
      end
    end
    
    # we add the Stream objects to the MediaInfo object
    mediainfo_object.streams << stream
  }
end