Module: MediaInfo
- Defined in:
- lib/mediainfo.rb,
lib/mediainfo/errors.rb,
lib/mediainfo/tracks.rb,
lib/mediainfo/version.rb
Defined Under Namespace
Classes: BadInputError, EnvironmentError, ExecutionError, IncompatibleVersionError, InvalidTrackAttributeValue, InvalidTrackType, NoStreamsForProxyError, RemoteUrlError, SingleStreamAPIError, Tracks, UnknownVersionError
Constant Summary
collapse
- VERSION =
'1.1.2'
Class Method Summary
collapse
Class Method Details
.from(input) ⇒ Object
52
53
54
55
56
|
# File 'lib/mediainfo.rb', line 52
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
|
.from_link(input) ⇒ Object
79
80
81
|
# File 'lib/mediainfo.rb', line 79
def self.from_link(input)
from_uri(URI(input))
end
|
.from_local_file(input) ⇒ Object
69
70
71
72
73
74
75
76
77
|
# File 'lib/mediainfo.rb', line 69
def self.from_local_file(input)
absolute_path = File.expand_path(input)
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
|
.from_string(input) ⇒ Object
58
59
60
61
62
63
|
# File 'lib/mediainfo.rb', line 58
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
|
.from_uri(input) ⇒ Object
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/mediainfo.rb', line 83
def self.from_uri(input)
http = Net::HTTP.new(input.host, input.port) request = Net::HTTP::Head.new(input.request_uri)
http.use_ssl = true if input.is_a? URI::HTTPS
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
|
.from_xml(input) ⇒ Object
65
66
67
|
# File 'lib/mediainfo.rb', line 65
def self.from_xml(input)
MediaInfo::Tracks.new(input)
end
|
.location ⇒ Object
Allow user to set custom mediainfo_path with ENV
10
11
12
13
14
|
# File 'lib/mediainfo.rb', line 10
def self.location
ENV['MEDIAINFO_PATH'].nil? ? mediainfo_location = '/usr/local/bin/mediainfo' : mediainfo_location = ENV['MEDIAINFO_PATH']
raise EnvironmentError, "#{mediainfo_location} cannot be found. Are you sure mediainfo is installed?" unless ::File.exist? mediainfo_location
return mediainfo_location
end
|
.run(input = nil) ⇒ Object
42
43
44
45
46
47
48
49
50
|
# File 'lib/mediainfo.rb', line 42
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
|
.set_singleton_method(object, name, parameters) ⇒ Object
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/mediainfo.rb', line 94
def self.set_singleton_method(object,name,parameters)
name.gsub!('.','_') if name.include?('.') name.downcase!
object.instance_variable_set("@#{name}",parameters)
object.define_singleton_method name do
object.instance_variable_get "@#{name}"
end
end
|
.version ⇒ Object
Allow collection of MediaInfo version details
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/mediainfo.rb', line 17
def self.version
version ||= `#{location} --Version`[/v([\d.]+)/, 1]
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
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
|
.xml_parser ⇒ Object
32
33
34
35
36
37
38
39
40
|
# File 'lib/mediainfo.rb', line 32
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
|