Class: Mediainfo

Inherits:
Object
  • Object
show all
Extended by:
AttrReaders
Defined in:
lib/mediainfo.rb,
lib/mediainfo/attr_readers.rb

Overview

# Mediainfo

Mediainfo is a class wrapping [the mediainfo CLI](mediainfo.sourceforge.net).

## Installation

$ gem install mediainfo -s http://gemcutter.org

## Usage

info = Mediainfo.new "/path/to/file"

That will issue the system call to ‘mediainfo` and parse the output. You can specify an alternate path if necessary:

Mediainfo.path = "/opt/local/bin/mediainfo"

By default, REXML is used as the XML parser. If you’d like, you can configure Mediainfo to use Hpricot or Nokogiri instead using one of the following approaches:

* define the `MEDIAINFO_XML_PARSER` environment variable to be the 
  name of the parser as you'd pass to a :gem or :require call. 

  e.g. `export MEDIAINFO_XML_PARSER=nokogiri`

* assign to Mediainfo.xml_parser after you've loaded the gem, 
  following the same naming conventions mentioned previously.

  e.g. `Mediainfo.xml_parser = "hpricot"`

Once you’ve got an instance setup, you can call numerous methods to get a variety of information about a file. Some attributes may be present for some files where others are not, but any supported attribute should at least return ‘nil`.

For a list of all possible attributes supported:

Mediainfo.supported_attributes

## Requirements

This requires at least the following version of the Mediainfo CLI:

MediaInfo Command line,
MediaInfoLib - v0.7.25

Previous versions of this gem(<= 0.5.1) worked against v0.7.11, which did not generate XML output, and is no longer supported.

Defined Under Namespace

Modules: AttrReaders Classes: Error, ExecutionError, IncompatibleVersionError

Constant Summary collapse

SECTIONS =

and General

%w( audio video image )

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AttrReaders

mediainfo_attr_reader, mediainfo_date_reader, mediainfo_duration_reader, mediainfo_int_reader, mediainfo_section_query, supported_attributes

Constructor Details

#initialize(full_filename = nil) ⇒ Mediainfo

Returns a new instance of Mediainfo.



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/mediainfo.rb', line 225

def initialize(full_filename = nil)
  if mediainfo_version < "0.7.25"
    raise IncompatibleVersionError,
      "Your version of mediainfo, #{mediainfo_version}, " +
      "is not compatible with this gem. >= 0.7.25 required."
  end
  
  if full_filename
    @full_filename = File.expand_path full_filename
    @path          = File.dirname  @full_filename
    @filename      = File.basename @full_filename
    
    raise ArgumentError, "need a path to a video file, got nil" unless @full_filename
    raise ArgumentError, "need a path to a video file, #{@full_filename} does not exist" unless File.exist? @full_filename
    
    @escaped_full_filename = @full_filename.shell_escape
    
    self.raw_response = mediainfo!
  end
end

Class Attribute Details

.pathObject

Returns the value of attribute path.



254
255
256
# File 'lib/mediainfo.rb', line 254

def path
  @path
end

.xml_parserObject

Returns the value of attribute xml_parser.



266
267
268
# File 'lib/mediainfo.rb', line 266

def xml_parser
  @xml_parser
end

Instance Attribute Details

#escaped_full_filenameObject (readonly)

Returns the value of attribute escaped_full_filename.



210
211
212
# File 'lib/mediainfo.rb', line 210

def escaped_full_filename
  @escaped_full_filename
end

#filenameObject (readonly)

Returns the value of attribute filename.



210
211
212
# File 'lib/mediainfo.rb', line 210

def filename
  @filename
end

#full_filenameObject (readonly)

Returns the value of attribute full_filename.



210
211
212
# File 'lib/mediainfo.rb', line 210

def full_filename
  @full_filename
end

#last_commandObject (readonly)

Returns the value of attribute last_command.



287
288
289
# File 'lib/mediainfo.rb', line 287

def last_command
  @last_command
end

#parsed_responseObject (readonly)

Returns the value of attribute parsed_response.



210
211
212
# File 'lib/mediainfo.rb', line 210

def parsed_response
  @parsed_response
end

#pathObject (readonly)

Returns the value of attribute path.



210
211
212
# File 'lib/mediainfo.rb', line 210

def path
  @path
end

#raw_responseObject

Returns the value of attribute raw_response.



210
211
212
# File 'lib/mediainfo.rb', line 210

def raw_response
  @raw_response
end

Class Method Details

.default_mediainfo_path!Object



280
# File 'lib/mediainfo.rb', line 280

def self.default_mediainfo_path!; self.path = "mediainfo"; end

.load_xml_parser!(parser = xml_parser) ⇒ Object



256
257
258
259
260
261
262
263
264
# File 'lib/mediainfo.rb', line 256

def load_xml_parser!(parser = xml_parser)
  begin
    gem     parser
    require parser
  rescue Gem::LoadError => e
    raise Gem::LoadError,
      "your specified XML parser, #{parser.inspect}, could not be loaded: #{e}"
  end
end

.versionObject



219
220
221
# File 'lib/mediainfo.rb', line 219

def self.version
  @version ||= `#{path} --Version`[/v([\d.]+)/, 1]
end

Instance Method Details

#audio_sample_rateObject Also known as: audio_sampling_rate



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/mediainfo.rb', line 162

def audio_sample_rate
  return unless rate = audio_sampling_rate_before_type_cast
  number = rate.gsub(/[^\d.]+/, "").to_f
  number = case rate
  when /KHz/ then number * 1000
  when /Hz/  then number
  else
    raise "unhandled sample rate! please report bug!"
  end
  number.to_i
end

#cbr?Boolean

Returns:

  • (Boolean)


94
# File 'lib/mediainfo.rb', line 94

def cbr?; video? and "Constant" == video_bit_rate_mode; end

#fpsObject Also known as: framerate



125
# File 'lib/mediainfo.rb', line 125

def fps; video_frame_rate[/[\d.]+/].to_f if video?; end

#heightObject



148
# File 'lib/mediainfo.rb', line 148

def height; if video?; video_height; elsif image?; image_height; end; end

#inspectObject



289
290
291
# File 'lib/mediainfo.rb', line 289

def inspect
  super.sub /@raw_response=".+?", @/, %{@raw_response="...", @}
end

#interlaced?Boolean

Returns:

  • (Boolean)


99
# File 'lib/mediainfo.rb', line 99

def interlaced?;  video? and "Interlaced" == video_scan_type; end

#max_fpsObject Also known as: max_framerate



133
# File 'lib/mediainfo.rb', line 133

def max_fps; video_maximum_frame_rate[/[\d.]+/].to_f if video?; end

#mediainfo_versionObject



283
284
285
# File 'lib/mediainfo.rb', line 283

def mediainfo_version
  self.class.version
end

#min_fpsObject Also known as: min_framerate



129
# File 'lib/mediainfo.rb', line 129

def min_fps; video_minimum_frame_rate[/[\d.]+/].to_f if video?; end

#mono?Boolean

Returns:

  • (Boolean)


194
# File 'lib/mediainfo.rb', line 194

def mono?;   1 == audio_channels; end

#progressive?Boolean

Returns:

  • (Boolean)


100
# File 'lib/mediainfo.rb', line 100

def progressive?; video? and not interlaced? end

#resolutionObject



146
# File 'lib/mediainfo.rb', line 146

def resolution; "#{width}x#{height}" if video? or image?; end

#sizeObject



75
# File 'lib/mediainfo.rb', line 75

def size; File.size(@full_filename) if @full_filename; end

#stereo?Boolean

Returns:

  • (Boolean)


193
# File 'lib/mediainfo.rb', line 193

def stereo?; 2 == audio_channels; end

#vbr?Boolean

Returns:

  • (Boolean)


95
# File 'lib/mediainfo.rb', line 95

def vbr?; video? and not cbr?; end

#widthObject



147
# File 'lib/mediainfo.rb', line 147

def width;  if video?; video_width;  elsif image?; image_width;  end; end

#xml_parserObject



278
# File 'lib/mediainfo.rb', line 278

def xml_parser; self.class.xml_parser; end