lib/mediainfo.rb in mediainfo-1.1.0 vs lib/mediainfo.rb in mediainfo-1.1.1
- old
+ new
@@ -37,11 +37,10 @@
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
@@ -49,47 +48,49 @@
end
return raw_response
end
def self.from(input)
- input_guideline_message = 'Bad Input' + "\n" + "Input must be: \n" +
- "A video or xml file location. Example: '~/videos/test_video.mov' or '~/videos/test_video.xml' \n" +
- "A valid URL. Example: 'http://www.site.com/videofile.mov' \n" +
- "Or MediaInfo XML \n"
- if input # User must specify file
- if input.include?('<?xml') # Must be first, else we could parse input (raw xml) with a URL in it and think it's a URL
- return MediaInfo::Tracks.new(input)
- elsif input.downcase.include?('http') || input.downcase.include?('www') # Handle Url Parsing
- @uri = URI(input)
- # Check if URL is valid
- http = ::Net::HTTP.new(@uri.host,@uri.port)
- request = Net::HTTP::Head.new(@uri.request_uri) # Only grab the Headers to be sure we don't try and download the whole file
- response = http.request(request)
- case response
- when Net::HTTPOK
- @escaped_input = URI.escape(@uri.to_s)
- else
- raise RemoteUrlError, "HTTP call to #{input} is not working!"
- end
- elsif input.include?('.xml')
- return MediaInfo::Tracks.new(::File.open(input).read)
- elsif !input.match(/[^\\]*\.\w+$/).nil? # A local file
- @file = ::File.expand_path input # turns ~/path/to/file into /home/user/path/to/file
- raise ArgumentError, 'You must include a file location.' if @file.nil?
- raise ArgumentError, "need a path to a video file, #{@file} does not exist" unless ::File.exist? @file
- @file_path = ::File.dirname @file
- @filename = ::File.basename @file
- @escaped_input = @file.shell_escape_double_quotes
- # Set variable for returned XML
- else
- raise ArgumentError, input_guideline_message
- end
- return MediaInfo::Tracks.new(MediaInfo.run(@escaped_input))
- else
- raise ArgumentError, input_guideline_message
- end
+ 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?('<?xml')
+ return from_link(input) if input =~ URI::regexp
+ return from_local_file(input) if input.match(/[^\\]*\.\w+$/)
+ raise BadInputError
+ end
+
+ def self.from_xml(input)
+ MediaInfo::Tracks.new(input)
+ end
+
+ def self.from_local_file(input)
+ absolute_path = File.expand_path(input) # turns relative to absolute path
+
+ raise ArgumentError, 'You must include a file location.' if absolute_path.nil?
+ raise ArgumentError, "need a path to a video file, #{absolute_path} does not exist" unless File.exist?(absolute_path)
+
+ return from_xml(File.open(absolute_path).read) if absolute_path.match(/[^\\]*\.(xml)$/)
+ MediaInfo::Tracks.new(MediaInfo.run(absolute_path.shell_escape_double_quotes))
+ end
+
+ def self.from_link(input)
+ from_uri(URI(input))
+ end
+
+ def self.from_uri(input)
+ http = Net::HTTP.new(input.host, input.port) # Check if input is valid
+ request = Net::HTTP::Head.new(input.request_uri) # Only grab the Headers to be sure we don't try and download the whole file
+
+ http.use_ssl = true if input.is_a? URI::HTTPS # For https support
+
+ raise RemoteUrlError, "HTTP call to #{input} is not working!" unless http.request(request).is_a?(Net::HTTPOK)
+
+ MediaInfo::Tracks.new(MediaInfo.run(URI.escape(input.to_s)))
+ end
def self.set_singleton_method(object,name,parameters)
# Handle parameters with invalid characters (instance_variable_set throws error)
name.gsub!('.','_') if name.include?('.') ## period in name
name.downcase!