sbraz/pymediainfo

Progress bar for `parse_speed=1`?

pcroland opened this issue · 4 comments

Is it possible somehow to access how much of the file is already read so I could hook it up to a rich.Progress?

sbraz commented

Currently, it is not possible. @JeromeMartinez, any idea how we could implement this? Is there some way to connect a callback function?

No callback function (yet), we have a callback function feature but there is no callback for a progress bar yet.
We have MediaInfo::State_Get(), value is between 0 and 10000, but you need a separate thread for probing the value.

Could I pipe the file through something that can read how many bytes has been read?

sbraz commented

Yes, the parse method accepts a file-like object so you could create a file-like object that does something every time its read() method is called.
Please take a look at the code if you want to understand how pymediainfo handles file-like objects:

try:
filename.seek(0, 2)
file_size = filename.tell()
filename.seek(0)
except AttributeError: # filename is not a file-like object
file_size = None
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"):
raise ValueError("File should be opened in binary mode")
lib.MediaInfo_Open_Buffer_Init(handle, file_size, 0)
while True:
buffer = filename.read(buffer_size)
if buffer:
# https://github.com/MediaArea/MediaInfoLib/blob/v20.09/Source/MediaInfo/File__Analyze.h#L1429
# 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)

On a side note, I just noticed that pymediainfo only seems to support seekable objects, so I created #132.