MediaInfo

MediaInfo is a class wrapping the mediainfo CLI.

Installation

$ gem install mediainfo

Usage

Parsing raw XML

media_info = MediaInfo.from(File.open('iphone6+_video.mov.xml').read)

Handling a local file

media_info = MediaInfo.from('~/Desktop/test.mov')

Handling a URL

media_info = MediaInfo.from('http://techslides.com/demos/sample-videos/small.mp4')

Ensure mediainfo is installed and within your PATH. You can specify an alternate path for the mediainfo binary if needed:

ENV['MEDIAINFO_PATH'] = "/usr/bin/mediainfo"

Once you have an MediaInfo object, you can start inspecting tracks:

media_info.track_types       => ['general','video','audio']
media_info.track_types.count => 3
media_info.video?            => true
media_info.image?            => nil
media_info.image.filesize    => MethodNotFound exception

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

media_info.video.count    => 1
media_info.video.duration => 120 (seconds)

Sometimes you'll have more than one track of a given type: - The first track type name, or any track type with <ID>1</ID> will not contain '1'

    media_info.track_types                => ['general','video','video2','audio','other','other2']
    media_info.track_types.count          => 5
    media_info.video?                     => true
    media_info.image?                     => nil
    media_info.video.count                => 1
    media_info.video.duration             => 29855000
    media_info.video.display_aspect_ratio => 1.222
    media_info.other.count                => 2
    media_info.video2.duration            => 29855000

> 🕑 A note on time zones: > Whenever possible, mediainfo provides timestamps in UTC. > To convert UTC timestamps to your system time zone, use #getlocal: > > media_info.video.encoded_date.getlocal => 2018-03-30 05:12:08 -0700 > > If a *date attribute is already in your local time zone, > that means no time zone data was found, > and the given time zone may not be correct.

In order to support all possible MediaInfo variations, you may see the following situation:

media_info.track_types => ['general','video','video5','audio','other','other2']

The track type media_info.video5 is available, but no video2, 3, and 4. This is because the MediaInfo from the video has:

<track type="Video">
    <ID>1</ID>
    ...
<track type="Video">
    <ID>5</ID>
    ...

The ID will take priority for labeling. Else if no ID exists, you'll see consecutive numbering for duplicate tracks in the Media.

Any second level attributes are also available:

MediaInfo.from('~/Desktop/test.mov').general.extra
=> #<MediaInfo::Tracks::Attributes::Extra:0x00007fa89f13aa98
 @com_apple_quicktime_creationdate=2018-03-30 08:12:08 -0400,
 @com_apple_quicktime_location_iso6709="+39.0286-077.3958+095.957/",
 @com_apple_quicktime_make="Apple",
 @com_apple_quicktime_model=0,
 @com_apple_quicktime_software=11.2>

REXML is used as the XML parser by default. If you'd like, you can configure Mediainfo to use Nokogiri instead:

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.

Development

bundle install
irb -I ./lib -r mediainfo 
irb(main):002:0> ::MediaInfo.location
=> "/usr/local/bin/mediainfo"

Testing

bundle exec rspec

Requirements

TODO