pymediainfo package

Module contents

class pymediainfo.MediaInfo(xml, encoding_errors='strict')

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. Such 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(f.read())
Parameters:
  • xml (str) – XML output obtained from MediaInfo.
  • encoding_errors (str) – option to pass to str.encode()’s errors parameter before parsing xml.
Raises:

xml.etree.ElementTree.ParseError – if passed invalid XML.

Variables:

tracks

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'>

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, encoding_errors='strict', parse_speed=0.5, text=False, full=True, legacy_stream_display=False, mediainfo_options=None)

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")
Parameters:
  • filename (str or pathlib.Path) – path to the media file which will be analyzed. A URL can also be used if libmediainfo was compiled with CURL support.
  • 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.
  • encoding_errors (str) – option to pass to str.encode()’s errors parameter before parsing MediaInfo’s XML output.
  • parse_speed (float) – passed to the library as ParseSpeed, this option takes values between 0 and 1. A higher value will yield more precise results in some cases but will also increase parsing time.
  • text (bool) – if True, MediaInfo’s text output will be returned instead of a MediaInfo object.
  • full (bool) – display additional tags, including computer-readable values for sizes and durations.
  • legacy_stream_display (bool) – display additional information about streams.
  • mediainfo_options (dict) – additional options that will be passed to the MediaInfo_Option function, for example: {"Language": "raw"}
Return type:

str if text is True.

Return type:

MediaInfo otherwise.

Raises:
  • FileNotFoundError – if passed a non-existent file (Python ≥ 3.3), does not work on Windows.
  • IOError – if passed a non-existent file (Python < 3.3), does not work on Windows.
  • RuntimeError – if parsing fails, this should not happen unless libmediainfo itself fails.
to_data()

Returns a dict representation of the object’s Tracks.

Return type:dict
to_json()

Returns a JSON representation of the object’s Tracks.

Return type:str
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.

Example:

>>> t = mi.tracks[0]
>>> t
<Track track_id='None', track_type='General'>
>>> t.duration
3000
>>> 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)
NoneType

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

to_data()

Returns a dict representation of the track attributes.

Example:

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