pymediainfo package

Module contents

class pymediainfo.MediaInfo(xml)

An object containing information about a media file.

MediaInfo objects can be created by directly calling code from libmediainfo (in this case, the library must be present on the system):

>>> pymediainfo.MediaInfo.parse("/path/to/file.mp4")

Alternatively, objects may be created from MediaInfo’s XML output. XML output can be obtained using the XML output format on versions older than v17.10 and the OLDXML format on newer versions.

Using such an XML file, we can create a MediaInfo object:

>>> with open("output.xml") as f:
...     mi = pymediainfo.MediaInfo(
Parameters:xml (str) – XML output obtained from MediaInfo
classmethod can_parse(library_file=None)

Checks whether media files can be analyzed using libmediainfo.

Return type:bool
classmethod parse(filename, library_file=None, cover_data=False)

Analyze a media file using libmediainfo. If libmediainfo is located in a non-standard location, the library_file parameter can be used:

>>> pymediainfo.MediaInfo.parse("tests/data/sample.mkv",
...     library_file="/path/to/libmediainfo.dylib")
  • filename (str or pathlib.Path) – path to the media file which will be analyzed.
  • library_file (str) – path to the libmediainfo library, this should only be used if the library cannot be auto-detected.
  • cover_data (bool) – whether to retrieve cover data as base64.
Return type:



Returns a dict representation of the object’s Tracks.

Return type:dict

Returns a json representation of the object’s Tracks.

Return type:str

A list of Track objects which the media file contains.

For instance:

>>> mi = pymediainfo.MediaInfo.parse("/path/to/file.mp4")
>>> for t in mi.tracks:
...     print(t)
<Track track_id='None', track_type='General'>
<Track track_id='1', track_type='Text'>
class pymediainfo.Track(xml_dom_fragment)

An object associated with a media file track.

Each Track attribute corresponds to attributes parsed from MediaInfo’s output. All attributes are lower case. Attributes that are present several times such as Duration yield a second attribute starting with other_ which is a list of all alternative attribute values.

When a non-existing attribute is accessed, None is returned.


>>> t = mi.tracks[0]
>>> t
<Track track_id='None', track_type='General'>
>>> t.duration
>>> t.to_data()["other_duration"]
['3 s 0 ms', '3 s 0 ms', '3 s 0 ms',
    '00:00:03.000', '00:00:03.000']
>>> type(t.non_existing)

All available attributes can be obtained by calling to_data().


Returns a dict representation of the track attributes.


>>> sorted(track.to_data().keys())[:3]
['codec', 'codec_extensions_usually_used', 'codec_url']
>>> t.to_data()["file_size"]
Return type:dict