pymediainfo
pymediainfo copied to clipboard
parse: add support for non-seekable streams
Currently, the parse()
method supports file-like objects but they have to be seekable:
https://github.com/sbraz/pymediainfo/blob/5ccb77050af8d88feae5847e584ca1652d25e077/pymediainfo/init.py#L480-L483
The mediainfo
binary is able to parse piped input so I assume the library supports non-seekable streams.
@jeromemartinez, how would I go about implementing this? Do I naively ignore MediaInfo_Open_Buffer_Continue_GoTo_Get
and pass an arbitrary size to MediaInfo_Open_Buffer_Continue
like this? It works with a test stream but I'm not sure it's the right way :D
diff --git a/pymediainfo/__init__.py b/pymediainfo/__init__.py
index 5cce5fa..852c2b3 100644
--- a/pymediainfo/__init__.py
+++ b/pymediainfo/__init__.py
@@ -457,13 +457,8 @@ class MediaInfo:
)
for option_name, option_value in mediainfo_options.items():
lib.MediaInfo_Option(handle, option_name, option_value)
- try:
- filename.seek(0, 2)
- file_size = filename.tell()
- filename.seek(0)
- except AttributeError: # filename is not a file-like object
- file_size = None
+ file_size = 2**64 - 1
if file_size is not None: # We have a file-like object, use the buffer protocol:
# Some file-like objects do not have a mode
if "b" not in getattr(filename, "mode", "b"):
@@ -476,13 +471,6 @@ class MediaInfo:
# 4th bit = finished
if lib.MediaInfo_Open_Buffer_Continue(handle, buffer, len(buffer)) & 0x08:
break
- # Ask MediaInfo if we need to seek
- seek = lib.MediaInfo_Open_Buffer_Continue_GoTo_Get(handle)
- # https://github.com/MediaArea/MediaInfoLib/blob/v20.09/Source/MediaInfoDLL/MediaInfoJNI.cpp#L127
- if seek != ctypes.c_uint64(-1).value:
- filename.seek(seek)
- # Inform MediaInfo we have sought
- lib.MediaInfo_Open_Buffer_Init(handle, file_size, filename.tell())
else:
break
lib.MediaInfo_Open_Buffer_Finalize(handle)