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 theOLDXML
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 (Python ≥ 2.7).
- xml.parsers.expat.ExpatError – if passed invalid XML (Python 2.6).
-
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')¶ 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.
Return type: 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.
-
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
-