require 'forwardable' require 'net/http' require 'mediainfo/errors' require 'mediainfo/tracks' require 'mediainfo/string' module MediaInfo def self.which(cmd) exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| exts.each { |ext| exe = File.join(path, "#{cmd}#{ext}") return exe if File.executable?(exe) && !File.directory?(exe) } end return nil end # Allow user to set custom mediainfo_path with ENV['MEDIAINFO_PATH'] def self.location if ENV['MEDIAINFO_PATH'].nil? mediainfo_location = which('mediainfo') else mediainfo_location = ENV['MEDIAINFO_PATH'] end raise EnvironmentError, "MediaInfo binary cannot be found. Are you sure mediainfo is installed?" if which('mediainfo').nil? raise EnvironmentError, "MediaInfo path you provided cannot be found. Please check your mediainfo installation location..." unless ::File.exist? mediainfo_location return mediainfo_location end # Allow collection of MediaInfo version details def self.version version ||= `#{location} --Version`[/v([\d.]+)/, 1] # Ensure MediaInfo isn't buggy and returns something raise UnknownVersionError, 'Unable to determine mediainfo version. ' + "We tried: #{location} --Version." + 'Set MediaInfo.path = \'/full/path/of/mediainfo\' if it is not in your PATH.' unless version # Ensure you're not using an old version of MediaInfo if version < '0.7.25' raise IncompatibleVersionError, "Your version of mediainfo, #{version}, " + 'is not compatible with this gem. >= 0.7.25 required.' else @version = version end end def self.xml_parser ENV['MEDIAINFO_XML_PARSER'].nil? || ENV['MEDIAINFO_XML_PARSER'].to_s.strip.empty? ? xml_parser = 'rexml/document' : xml_parser = ENV['MEDIAINFO_XML_PARSER'] begin require xml_parser rescue Gem::LoadError => ex raise Gem::LoadError, "Your specified XML parser, #{xml_parser.inspect}, could not be loaded: #{ex.message}" end return xml_parser end def self.run(input = nil) raise ArgumentError, 'Your input cannot be blank.' if input.nil? command = "#{location} #{input} --Output=XML 2>&1" raw_response = `#{command}` unless $? == 0 raise ExecutionError, "Execution of '#{command}' failed. #{raw_response.inspect}" end return raw_response end def self.from(input) return from_uri(input) if input.is_a?(URI) return from_string(input) if input.is_a?(String) raise BadInputError end def self.from_string(input) return from_xml(input) if input.include?('