module MediaInfo

Constants

VERSION

Public Class Methods

from(input) click to toggle source
   # File lib/mediainfo.rb
59 def self.from(input)
60   return from_uri(input) if input.is_a?(URI)
61   return from_string(input) if input.is_a?(String)
62   raise BadInputError
63 end
from_local_file(input) click to toggle source
   # File lib/mediainfo.rb
76 def self.from_local_file(input)
77   absolute_path = File.expand_path(input) # turns relative to absolute path
78 
79   raise ArgumentError, 'You must include a file location.' if absolute_path.nil?
80   raise ArgumentError, "need a path to a video file, #{absolute_path} does not exist" unless File.exist?(absolute_path)
81 
82   return from_xml(File.open(absolute_path).read) if absolute_path.match(/[^\\]*\.(xml)$/)
83   MediaInfo::Tracks.new(MediaInfo.run(absolute_path.shell_escape_double_quotes))
84 end
from_string(input) click to toggle source
   # File lib/mediainfo.rb
65 def self.from_string(input)
66   return from_xml(input) if input.include?('<?xml')
67   return from_link(input) if input.include?('://') && input =~ URI::regexp
68   return from_local_file(input) if input.match(/[^\\]*\.\w+$/)
69   raise BadInputError
70 end
from_uri(input) click to toggle source
    # File lib/mediainfo.rb
100 def self.from_uri(input)
101   if input.host.include?('amazonaws.com')
102     MediaInfo::Tracks.new(MediaInfo.run(input.to_s)) # Removed URI.escape due to Error parsing the X-Amz-Credential parameter; the Credential is mal-formed; expecting "&lt;YOUR-AKID&gt;/YYYYMMDD/REGION/SERVICE/aws4_request"
103   else
104     http = Net::HTTP.new(input.host, input.port) # Check if input is valid
105     request = Net::HTTP::Head.new(input.request_uri) # Only grab the Headers to be sure we don't try and download the whole file; Doesn't work with presigned_urls in aws/s3
106     http.use_ssl = true if input.is_a? URI::HTTPS # For https support
107     http_request = http.request(request)
108     raise RemoteUrlError, "HTTP call to #{input} is not working : #{http_request.value}" unless http_request.is_a?(Net::HTTPOK)
109     MediaInfo::Tracks.new(MediaInfo.run(URI.escape(input.to_s)))
110   end
111 end
from_xml(input) click to toggle source
   # File lib/mediainfo.rb
72 def self.from_xml(input)
73   MediaInfo::Tracks.new(input)
74 end
location() click to toggle source

Allow user to set custom mediainfo_path with ENV

   # File lib/mediainfo.rb
22 def self.location
23   if ENV['MEDIAINFO_PATH'].nil?
24     mediainfo_location = which('mediainfo')
25   else
26     mediainfo_location = ENV['MEDIAINFO_PATH']
27     raise EnvironmentError, "MediaInfo path you provided cannot be found. Please check your mediainfo installation location..." unless ::File.exist? mediainfo_location
28   end
29   raise EnvironmentError, "MediaInfo binary cannot be found. Are you sure mediainfo is installed?" if mediainfo_location.nil? || mediainfo_location.empty?
30   return mediainfo_location
31 end
run(input = nil) click to toggle source
   # File lib/mediainfo.rb
90 def self.run(input = nil)
91   raise ArgumentError, 'Your input cannot be blank.' if input.nil?
92   command = "#{location} \"#{input}\" --Output=XML"
93   raw_response, errors, status = Open3.capture3(command)
94   unless status.exitstatus == 0
95     raise ExecutionError, "Execution of '#{command}' failed: \n #{errors.red}"
96   end
97   return raw_response
98 end
set_singleton_method(object,name,parameters) click to toggle source
    # File lib/mediainfo.rb
113 def self.set_singleton_method(object,name,parameters)
114   # Handle parameters with invalid characters (instance_variable_set throws error)
115   name.gsub!('.','_') if name.include?('.') ## period in name
116   name.downcase!
117   # Create singleton_method
118   object.instance_variable_set("@#{name}",parameters)
119   object.define_singleton_method name do
120     object.instance_variable_get "@#{name}"
121   end
122 end
version() click to toggle source

Allow collection of MediaInfo version details

   # File lib/mediainfo.rb
34 def self.version
35   version ||= `#{location} --Version`[/v([\d.]+)/, 1]
36   # Ensure MediaInfo isn't buggy and returns something
37   raise UnknownVersionError, 'Unable to determine mediainfo version. ' + "We tried: #{location} --Version." +
38         'Set MediaInfo.path = \'/full/path/of/mediainfo\' if it is not in your PATH.' unless version
39   # Ensure you're not using an old version of MediaInfo
40   if version < '0.7.25'
41     raise IncompatibleVersionError, "Your version of mediainfo, #{version}, " +
42         'is not compatible with this gem. >= 0.7.25 required.'
43   else
44     @version = version
45   end
46 
47 end
which(cmd) click to toggle source
   # File lib/mediainfo.rb
10 def self.which(cmd)
11   exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
12   ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
13     exts.each { |ext|
14       exe = File.join(path, "#{cmd}#{ext}")
15       return exe if File.executable?(exe) && !File.directory?(exe)
16     }
17   end
18   return nil
19 end
xml_parser() click to toggle source
   # File lib/mediainfo.rb
49 def self.xml_parser
50   ENV['MEDIAINFO_XML_PARSER'].nil? || ENV['MEDIAINFO_XML_PARSER'].to_s.strip.empty? ? xml_parser = 'rexml/document' : xml_parser = ENV['MEDIAINFO_XML_PARSER']
51   begin
52     require xml_parser
53   rescue Gem::LoadError => ex
54     raise Gem::LoadError, "Your specified XML parser, #{xml_parser.inspect}, could not be loaded: #{ex.message}"
55   end
56   return xml_parser
57 end