Ruby wrapper for the mediainfo CLI.
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib pull/36: Fix MediaInfo::Tracks.initialize issue with false xml input Sep 25, 2018
spec Fix MediaInfo::Tracks.initialize issue with false xml input. (#36) Sep 25, 2018
.gitignore Improve specs & fix MediaInfo::Tracks 'type?' methods definition (#29) Aug 31, 2018
.rspec v1.0.0 May 22, 2018
.travis.yml config which installs mediainfo in the Travis env... in theory... Oct 18, 2018
Changelog v1.0.0 May 22, 2018
Gemfile Travis isn't having any weird casing with your Gemfile. Oct 18, 2018
LICENSE i'm not using at the moment Oct 18, 2018
Rakefile v1.0.0 May 22, 2018

MediaInfo Build Status

MediaInfo is a class wrapping the mediainfo CLI.


$ gem install mediainfo


Parsing raw XML

media_info = MediaInfo.from('').read)

Handling a local file

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

Handling a URL

media_info = MediaInfo.from('')

You can specify an alternate path for the MediaInfo Binary:

ENV['MEDIAINFO_PATH'] = "/opt/local/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            => 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:    => 1 => 120 (seconds)

Sometimes you'll have more than one track of a given type:

  • The first track type name, or any track type with 1 will not contain '1'

     media_info.track_types                => ['general','video','video2','audio','other','other2']
     media_info.track_types.count          => 5                     => true
     media_info.image?                     => nil                => 1             => 29855000 => 1.222
     media_info.other.count                => 2
     media_info.video2.duration            => 29855000
  • Note that the above automatically converts MediaInfo Strings into Time, Integer, and Float objects:         => Time
      media_info.video2.duration.class            => Integer => Float
  • Any track attribute name with "date" and matching /\d-/ will be converted using Time.parse: => 2018-03-30 12:12:08 -0400   => 2016-02-10 01:00:00 -0600
  • .duration and .overall_duration will be returned as milliseconds AS LONG AS the Duration and Overall_Duration match one of the expected units (each separated by a space or not):

    • h (<Duration>15h</Duration>) (hour)

    • hour (<Duration>15hour</Duration>)

    • mn (<Duration>15hour 6mn</Duration>) (minute)

    • m (<Duration>15hour 6m</Duration>) (minute)

    • min (<Duration>15hour 6min</Duration>) (minute)

    • s (<Duration>15hour 6min 59s</Duration>) (second)

    • sec (<Duration>15hour 6min 59sec</Duration>) (second)

    • ms (<Duration>15hour 6min 59sec 301ms</Duration>) (milliseconds)

    • in the form of a Float (as for iphone mov files)

    • Submit an issue to add more! => 15164 (\<Duration>15s 164ms\</Duration>) => 36286 (\<Duration>36s 286ms\</Duration>) => 123456 (\<Duration>123.456\</Duration>)
  • We standardize the naming of several Attributes:

    • You can review lib/attribute_standardization_rules.yml to see them all => nil (\<Bit_rate>41.2 Mbps\</Bit_rate>) => "41.2 Mbps" (\<Bit_rate>41.2 Mbps\</Bit_rate>)
        media_info.general.filesize => "11.5 MiB" (\<File_size>11.5 MiB\</File_size>

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">
<track type="Video">

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::Tracks::Attributes::Extra:0x00007fa89f13aa98
 @com_apple_quicktime_creationdate=2018-03-30 08:12:08 -0400,

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

  • 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

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.


  • Gem version 1.0.0 has been tested on v18.03.1
  • Gem versions < 1.0.0 require at least: MediaInfoLib v0.7.25
  • Gem versions <= 0.5.1 worked against MediaInfoLib v0.7.11, which did not generate XML output, and is no longer supported.