Skip to content

Commit

Permalink
Add an option to control parsing speed, fixes #65
Browse files Browse the repository at this point in the history
  • Loading branch information
sbraz committed Mar 5, 2019
1 parent 7e5cb31 commit ff57017
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pymediainfo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def can_parse(cls, library_file=None):
return False
@classmethod
def parse(cls, filename, library_file=None, cover_data=False,
encoding_errors="strict"):
encoding_errors="strict", parse_speed=0.5):
"""
Analyze a media file using libmediainfo.
If libmediainfo is located in a non-standard location, the `library_file` parameter can be used:
Expand All @@ -197,6 +197,10 @@ def parse(cls, filename, library_file=None, cover_data=False,
: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.
:type filename: str or pathlib.Path
:rtype: MediaInfo
:raises FileNotFoundError: if passed a non-existent file
Expand Down Expand Up @@ -255,6 +259,7 @@ def parse(cls, filename, library_file=None, cover_data=False,
locale.setlocale(locale.LC_CTYPE, locale.getdefaultlocale())
lib.MediaInfo_Option(None, "Inform", xml_option)
lib.MediaInfo_Option(None, "Complete", "1")
lib.MediaInfo_Option(None, "ParseSpeed", str(parse_speed))
if lib.MediaInfo_Open(handle, filename) == 0:
raise RuntimeError("An eror occured while opening {0}"
" with libmediainfo".format(filename))
Expand Down
Binary file added tests/data/vbr_requires_parsespeed_1.mp4
Binary file not shown.
9 changes: 9 additions & 0 deletions tests/test_pymediainfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,12 @@ def test_parse_invalid_url(self):
# since non-existent files return a different exception
self.assertRaises(RuntimeError, MediaInfo.parse,
"unsupportedscheme://")

class MediaInfoSlowParseTest(unittest.TestCase):
def setUp(self):
self.mi = MediaInfo.parse(
os.path.join(data_dir, "vbr_requires_parsespeed_1.mp4"),
parse_speed=1
)
def test_slow_parse_speed(self):
self.assertEqual(self.mi.tracks[2].stream_size, "3353 / 45")

0 comments on commit ff57017

Please sign in to comment.