Search code examples
pythonpython-3.xvlclibvlc

Python3/vlc: how to get media stats (function get_stats)


I am trying to call the get_stats() function on a media:

>>> instance = vlc.Instance()
>>> media = instance.media_new('song.mp3')
>>> media.get_stats()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: get_stats() missing 1 required positional argument: 'p_stats'
>>> media.get_stats('input_bitrate')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/alain/Audio/vlc.py", line 2517, in get_stats
    return libvlc_media_get_stats(self, p_stats)
  File "/home/alain/Audio/vlc.py", line 5349, in libvlc_media_get_stats
    return f(p_md, p_stats)
ctypes.ArgumentError: argument 2: <class 'TypeError'>: expected LP_MediaStats instance instead of str

The expected argument type LP_MediaStats does not exist in vlc.py, but I found this:

class MediaStats(_Cstruct):
    _fields_ = [
        ('read_bytes',          ctypes.c_int  ),
        ('input_bitrate',       ctypes.c_float),
        ('demux_read_bytes',    ctypes.c_int  ),
        ('demux_bitrate',       ctypes.c_float),
        ('demux_corrupted',     ctypes.c_int  ),
        ('demux_discontinuity', ctypes.c_int  ),
        ('decoded_video',       ctypes.c_int  ),
        ('decoded_audio',       ctypes.c_int  ),
        ('displayed_pictures',  ctypes.c_int  ),
        ('lost_pictures',       ctypes.c_int  ),
        ('played_abuffers',     ctypes.c_int  ),
        ('lost_abuffers',       ctypes.c_int  ),
        ('sent_packets',        ctypes.c_int  ),
        ('sent_bytes',          ctypes.c_int  ),
        ('send_bitrate',        ctypes.c_float),
    ]

Do anyone know how to use this function?


Solution

  • Start with documentation here. It says:

    libvlc_media_get_stats(p_md, p_stats) Get the current statistics about the media.

    Parameters: p_md - : media descriptor object. p_stats - : structure that contain the statistics about the media (this structure must be allocated by the caller). Returns: true if the statistics are available, false otherwise \libvlc_return_bool.

    According to docs, you should use media descriptor as a parameter. Now we have to figure out how to get media descriptor.