libvlc / vlcj, Get video metadata (num of audio tracks) without playing the video

StackOverflow https://stackoverflow.com/questions/22260515

  •  11-06-2023
  •  | 
  •  

Question

I have a EmbeddedMediaPlayerComponent and I want to check before playing if the video has audio track.

The getMediaPlayer().getAudioTrackCount() method works fine but only when I play the video and I am inside the public void playing(MediaPlayer mp) event. I also tryed

getMediaPlayer().prepareMedia("/path/to/media", null);
getMediaPlayer().play();
System.out.println("TRACKS: "+getMediaPlayer().getAudioTrackCount());

But it does not work. it says 0.

I also tryed:

MediaPlayerFactory factory = new MediaPlayerFactory();
HeadlessMediaPlayer p = factory.newHeadlessMediaPlayer();
p.prepareMedia("/path/to/video", null);
p.parseMedia();
System.out.println("TRACKS: "+p.getAudioTrackCount());

But it also says -1. Is there a way I can do that ? or using another technique?

Was it helpful?

Solution

The track count is not metadata, so using parseMedia() here is not going to help.

parseMedia() will work to get e.g. ID3 tag data, title, artist, album, and so on.

The track data is usually not available until after the media has started playing, since it is the particular decoder plugin that knows how many tracks there are. Even then, it is not always available immediately after the media has started playing, sometimes there's an indeterminate delay (and no LibVLC event).

In applications where I need the track information before playing the media, I usually would use something like the native MediaInfo application and parse the output - this has a plain-text out format, or an XML output format and IIRC the newer versions have a JSON output format. The downside is you have to launch a native process to do this, I use CommonsExec for things like this. It's pretty simple and does work even though it's not a pure Java solution, but neither is vlcj!

A slight aside if you did actually want the meta data there is an easier way, just use this method on the MediaPlayerFactory:

public MediaMeta getMediaMeta(String mediaPath, boolean parse);

This gives you the meta data without having to prepare, play or parse media.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top