MediaInfo Build Status

MediaInfo is a class wrapping the mediainfo CLI.


$ gem install mediainfo


  • Versions > 1.3.0 support S3 URLs. See rspec test for example.

Parsing raw XML

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

Handling a local file

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

Handling a URL

media_info = MediaInfo.from('')

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            => 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 UTC => 2016-02-10 01:00:00 UTC

🕑 A note on time zones: Whenever possible, mediainfo provides timestamps in UTC. To convert UTC timestamps to your system time zone, use #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.

  • .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.


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


bundle exec rspec 


  • 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.


  • Use github actions to test
  • Replace URI.escape cause it's EOL
  • Mocks/Stubs for AWS code? Maybe just disable the tests instead and only test when changing the code?