2

I am trying to obtain bitrate, sample rate actual bits per sample of an audio file using python-vlc.

I saw that we can use libvlc_media_tracks_get() to obtain bitrate, but I am not sure how to get the others.

But even if this method can obtain all 3 info, I still can't manage to make the method work. It takes 2 arguments p_md and tracks. I don't understand what is tracks. It says it requires an instance of LP_LP_MediaTrack but I can't somehow find what that means.

Arkyo
  • 307
  • 1
  • 2
  • 13
  • Have you parsed or played the media first? – mfkl Sep 21 '20 at 17:18
  • Yes, I have done it – Arkyo Sep 21 '20 at 17:50
  • If you solved this problem on your own, then either wait a day and post a real answer if you think others will benefit from knowing how to do this, or delete the question if you think they'll be able to find the exact same information you did. Don't edit an answer into a question, that's not how SO works. – Mike 'Pomax' Kamermans Sep 21 '20 at 18:46
  • No, I haven't found my answer yet. Just because I figured out how to get bitrate doesn't mean I got the others. – Arkyo Sep 21 '20 at 18:47
  • Can you explain how can i print bitrate value with python-vlc module? – Chris P Sep 30 '20 at 01:23
  • bits_per_sample = audio_total_size_in_bits/(total_duration_in_seconds*sample_rate_hz) – Chris P Sep 30 '20 at 01:57
  • @ChrisP I have modified the question and posted an answer explaining it. – Arkyo Sep 30 '20 at 20:36

2 Answers2

1

Bitrate:

# This allocates a double pointer and a MediaTrack object in RAM, then points to
# it, but no value is assigned to this double pointer or the MediaTrack object.
mediaTrack_pp = ctypes.POINTER(vlc.MediaTrack)()

# Assigns the MediaTrack in the double pointer to the values stored in `media`,
# returning the amount of media tracks as `n`
n = vlc.libvlc_media_tracks_get(media, ctypes.byref(mediaTrack_pp))

# Converts the double pointer to `POINTER` class that Python understands. We can
# then get the value the pointer is pointing at.
info = ctypes.cast(mediaTrack_pp, ctypes.POINTER(ctypes.POINTER(vlc.MediaTrack) * n))

# This gets the actual value of the double pointer i.e. value of MediaTrack
media_tracks = info.contents[0].contents

Sample rate (appending the above code):

# According to the API doc, MediaTrack class has an `u` field which has an
# `audio` field, containing the audio track.
audio = ctypes.cast(media_tracks.u.audio, ctypes.POINTER(vlc.AudioTrack))

# The AudioTrack class has a `rate` field, which is the sample rate.
sample = audio.contents.rate

The main reason I couldn't get the bitrate is because I didn't understand how to get the data type LP_LP_MediaTrack. This is a double pointer of a MediaTrack instance.

When vlc.libvlc_media_tracks_get(media, ctypes.byref(mediaTrack_pp)) runs, I think the library simply sets copies the MediaTrack instance in media to the actual memory location thatmediaTrack_pp points to

We can then get the actual object in Python using the ctypes.cast() method. This algorithm should apply on all Python codes that uses LP_xxx data types, even for other libraries.

With that problem solved, all that left was to crunch the API documentation.
VLC MediaTrack class documentation

Arkyo
  • 307
  • 1
  • 2
  • 13
  • 1
    Bitrate information doesn't seem to be available in new versions of python-vlc (at the moment of writing this comment the newest version is 3.0.18121). The last version where bitrate information is available is 3.0.11115. – volkut Apr 03 '23 at 20:54
0

In libVlc try

MediaPlayer.Media.Tracks[0].Bitrate;
Dr. Best
  • 1
  • 1