Class: Mediainfo

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, 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

## 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"

Once you have an info object, you can start inspecting streams and general metadata.

info.streams.count # 2
info.audio?        # true
info.video?        # true
info.image?        # false

When inspecting specific types of streams, you have a couple general API options. The first approach assumes one stream of a given type, a common scenario in many video files, for example.

info.video.count    # 1
info.audio.count    # 1
info.video.duration # 120 (seconds)

Sometimes you’ll have more than one stream of a given type. Quicktime files can often contain artifacts like this from somebody editing a more ‘normal’ file.

info = Mediainfo.new "funky.mov"

info.video?            # true
info.video.count       # 2
info.video.duration    # raises SingleStreamAPIError !
info.video[0].duration # 120
info.video[1].duration # 10

For some more usage examples, please see the very reasonable test suite accompanying the source code for this library. It contains a bunch of relevant usage examples. More docs in the future.. contributions very welcome!

Moving on, REXML is used as the XML parser by default. 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: AudioStream, Error, ExecutionError, GeneralStream, ImageStream, IncompatibleVersionError, MenuStream, Stream, StreamProxy, TextStream, UnknownVersionError, VideoStream

Constant Summary collapse

SECTIONS =
[:general, :video, :audio, :image, :menu, :text]
NON_GENERAL_SECTIONS =
SECTIONS - [:general]

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

Constructor Details

#initialize(full_filename = nil) ⇒ Mediainfo

Returns a new instance of Mediainfo.



378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/mediainfo.rb', line 378

def initialize(full_filename = nil)
  unless mediainfo_version
    raise UnknownVersionError,
      "Unable to determine mediainfo version. " +
      "We tried: #{self.class.version_command} " +
      "Are you sure mediainfo is installed at #{self.class.path.inspect}? " + 
      "Set Mediainfo.path = /where/is/mediainfo if it is not in your PATH."
  end
  
  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
  
  @streams = []
  
  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_double_quotes
    
    self.raw_response = mediainfo!
  end
end

Class Attribute Details

.pathObject

Returns the value of attribute path.



417
418
419
# File 'lib/mediainfo.rb', line 417

def path
  @path
end

.xml_parserObject

Returns the value of attribute xml_parser.



429
430
431
# File 'lib/mediainfo.rb', line 429

def xml_parser
  @xml_parser
end

Instance Attribute Details

#escaped_full_filenameObject (readonly)

Returns the value of attribute escaped_full_filename.



374
375
376
# File 'lib/mediainfo.rb', line 374

def escaped_full_filename
  @escaped_full_filename
end

#filenameObject (readonly)

Returns the value of attribute filename.



374
375
376
# File 'lib/mediainfo.rb', line 374

def filename
  @filename
end

#full_filenameObject (readonly)

Returns the value of attribute full_filename.



374
375
376
# File 'lib/mediainfo.rb', line 374

def full_filename
  @full_filename
end

#last_commandObject (readonly)

Returns the value of attribute last_command.



448
449
450
# File 'lib/mediainfo.rb', line 448

def last_command
  @last_command
end

#pathObject (readonly)

Returns the value of attribute path.



374
375
376
# File 'lib/mediainfo.rb', line 374

def path
  @path
end

#raw_responseObject

Returns the value of attribute raw_response.



374
375
376
# File 'lib/mediainfo.rb', line 374

def raw_response
  @raw_response
end

#streamsObject (readonly)

Returns the value of attribute streams.



118
119
120
# File 'lib/mediainfo.rb', line 118

def streams
  @streams
end

Class Method Details

.default_mediainfo_path!Object



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

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

.delegate(method_name, stream_type = nil) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/mediainfo.rb', line 96

def self.delegate(method_name, stream_type = nil)
  if stream_type == :general
    def_delegator :"@#{stream_type}_stream", method_name
  else
    def_delegator :"@#{stream_type}_stream", method_name, "#{stream_type}_#{method_name}"
  end
end

.load_xml_parser!(parser = xml_parser) ⇒ Object



419
420
421
422
423
424
425
426
427
# File 'lib/mediainfo.rb', line 419

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

.supported_attributesObject

AttrReaders depends on this.



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

def self.supported_attributes; @supported_attributes ||= []; end

.versionObject



104
105
106
# File 'lib/mediainfo.rb', line 104

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

.version_commandObject



108
109
110
# File 'lib/mediainfo.rb', line 108

def self.version_command
  "#{path} --Version"
end

Instance Method Details

#inspectObject



450
451
452
# File 'lib/mediainfo.rb', line 450

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

#mediainfo_versionObject



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

def mediainfo_version; self.class.version; end

#sizeObject

Size of source file as reported by File.size. Returns nil if you haven’t yet fired off the system command.



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

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

#xml_parserObject



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

def xml_parser; self.class.xml_parser; end