in amplify/backend/function/MAPSMediaInfoLambdaLayer/opt/python/lib/python3.7/site-packages/pymediainfo/__init__.py [0:0]
def parse(cls, 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,
output=None):
"""
Analyze a media file using libmediainfo.
.. note::
Because of the way the underlying library works, this method should not
be called simultaneously from multiple threads *with different arguments*.
Doing so will cause inconsistencies or failures by changing
library options that are shared across threads.
:param filename: path to the media file which will be analyzed.
A URL can also be used if libmediainfo was compiled
with CURL support.
:param str library_file: path to the libmediainfo library, this should only be used if the library cannot be auto-detected.
:param bool cover_data: whether to retrieve cover data as base64.
:param str encoding_errors: option to pass to :func:`str.encode`'s `errors`
parameter before parsing MediaInfo's XML output.
:param float parse_speed: 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.
:param bool full: display additional tags, including computer-readable values
for sizes and durations.
:param bool legacy_stream_display: display additional information about streams.
:param dict mediainfo_options: additional options that will be passed to the `MediaInfo_Option` function,
for example: ``{"Language": "raw"}``. Do not use this parameter when running the
method simultaneously from multiple threads, it will trigger a reset of all options
which will cause inconsistencies or failures.
:param str output: custom output format for MediaInfo, corresponds to the CLI's
``--Output`` parameter. Setting this causes the method to
return a `str` instead of a :class:`MediaInfo` object.
Useful values include:
* the empty `str` ``""`` (corresponds to the default
text output, obtained when running ``mediainfo`` with no
additional parameters)
* ``"XML"``
* ``"JSON"``
* ``%``-delimited templates (see ``mediainfo --Info-Parameters``)
:type filename: str or pathlib.Path or os.PathLike
:rtype: str if `output` is set.
:rtype: :class:`MediaInfo` otherwise.
:raises FileNotFoundError: if passed a non-existent file
(Python ≥ 3.3), does not work on Windows.
:raises IOError: if passed a non-existent file (Python < 3.3),
does not work on Windows.
:raises RuntimeError: if parsing fails, this should not
happen unless libmediainfo itself fails.
Examples:
>>> pymediainfo.MediaInfo.parse("tests/data/sample.mkv")
<pymediainfo.MediaInfo object at 0x7fa83a3db240>
>>> import json
>>> mi = pymediainfo.MediaInfo.parse("tests/data/sample.mkv",
... output="JSON")
>>> json.loads(mi)["media"]["track"][0]
{'@type': 'General', 'TextCount': '1', 'FileExtension': 'mkv',
'FileSize': '5904', … }
"""
lib, handle, lib_version_str, lib_version = cls._get_library(library_file)
filename, is_url = cls._parse_filename(filename)
# Try to open the file (if it's not a URL)
# Doesn't work on Windows because paths are URLs
if not is_url:
# Test whether the file is readable
with open(filename, "rb"):
pass
# The XML option was renamed starting with version 17.10
if lib_version >= (17, 10):
xml_option = "OLDXML"
else:
xml_option = "XML"
# Cover_Data is not extracted by default since version 18.03
# See https://github.com/MediaArea/MediaInfoLib/commit/d8fd88a1c282d1c09388c55ee0b46029e7330690
if lib_version >= (18, 3):
lib.MediaInfo_Option(handle, "Cover_Data", "base64" if cover_data else "")
lib.MediaInfo_Option(handle, "CharSet", "UTF-8")
# Fix for https://github.com/sbraz/pymediainfo/issues/22
# Python 2 does not change LC_CTYPE
# at startup: https://bugs.python.org/issue6203
if (sys.version_info < (3,) and os.name == "posix"
and locale.getlocale() == (None, None)):
locale.setlocale(locale.LC_CTYPE, locale.getdefaultlocale())
if text:
warnings.warn('The "text" option is obsolete and will be removed '
'in the next major version. Use output="" instead.',
DeprecationWarning
)
output = ""
lib.MediaInfo_Option(handle, "Inform", xml_option if output is None else output)
lib.MediaInfo_Option(handle, "Complete", "1" if full else "")
lib.MediaInfo_Option(handle, "ParseSpeed", str(parse_speed))
lib.MediaInfo_Option(handle, "LegacyStreamDisplay", "1" if legacy_stream_display else "")
if mediainfo_options is not None:
if lib_version < (19, 9):
warnings.warn("This version of MediaInfo ({}) does not support resetting all options to their default values, "
"passing it custom options is not recommended and may result in unpredictable behavior, "
"see https://github.com/MediaArea/MediaInfoLib/issues/1128".format(lib_version_str),
RuntimeWarning
)
for option_name, option_value in mediainfo_options.items():
lib.MediaInfo_Option(handle, option_name, option_value)
if lib.MediaInfo_Open(handle, filename) == 0:
lib.MediaInfo_Close(handle)
lib.MediaInfo_Delete(handle)
raise RuntimeError("An eror occured while opening {}"
" with libmediainfo".format(filename))
info = lib.MediaInfo_Inform(handle, 0)
# Reset all options to their defaults so that they aren't
# retained when the parse method is called several times
# https://github.com/MediaArea/MediaInfoLib/issues/1128
# Do not call it when it is not required because it breaks threads
# https://github.com/sbraz/pymediainfo/issues/76#issuecomment-575245093
if mediainfo_options is not None and lib_version >= (19, 9):
lib.MediaInfo_Option(handle, "Reset", "")
# Delete the handle
lib.MediaInfo_Close(handle)
lib.MediaInfo_Delete(handle)
if output is None:
return cls(info, encoding_errors)
else:
return info