简体   繁体   中英

mediaPlayer.setSpu() not working

Currently I am working on some code based on VLCJ to play video content, which is working pretty fine, but I am struggling hard making the setSpu() method work.

Just to mention, when it comes to load an external subtitle, in a file apart from the video file, it is working fine. The problem appears when I try to play subtitles contained in the media file. (eg subs contained into a MKV file).

I read carefully GitHub post "setSpu not working #278 ", and I think that maybe the problem is that I am not invoking the setSpu() method correctly.

To make it simple, I am trying to make it works on the example "uk.co.caprica.vlcj.test.basic.TestPlayer".

On TestPlayer.java class, I loaded all native vlc required libs and configured the mediaPath, and mediaPlayer, so if I execute the class, the media player is built properly, and the video starts playing.

Now, to try make the subtitle work, I reused the button "subTitlesButton" on "PlayerControlsPanel.java". First of all, as the spu to be set is the ID of the TrackDescription, I added the following code, and executed to get the spuDescriptions list:

subTitlesButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            System.out.println(mediaPlayer.getSpuDescriptions());
        }
    });

When the Sub-titles button is pressed, the following output is get:

spuDescriptions=[TrackDescription[id=-1,description=Deshabilitar], TrackDescription[id=3,description=Pista 1 - [Español]], TrackDescription[id=4,description=Pista 2 - [Inglés]], TrackDescription[id=5,description=Pista 3 - [Español]]]

So, to keep it simple, I just tried to add the following code and execute it:

subTitlesButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            System.out.println(mediaPlayer.getSpuDescriptions());
            mediaPlayer.setSpu(3); // TrackDescription[id=3,description=Track 1 - [Spanish]]
        }
    });

The expected resault would be the subtitle "Track 1 - [Spanish]" with ID=3 to appear on screen, but nothing happens. The video goes on and is being played properly, but the sub-title is not shown.

All the other buttons, work fine when you pressed them, you get the expected result (pause, stop, play, fastforward, rewind, and so on)... so I dont get the point on why media.setSpu() is not working there.

Would be much appreciated some help :) Thanks in advance.

EDITED The exact problem was that all subtitles contained in the media file (video.mkv) were UTF8 text encoded. I tried to re-mount the video.mkv file with mkvmerge, but this program allways converts SRT files to UTF8 text format. WORKAROUND convert the SRT files to ASS subtitles format. If the video.mkv contains .ASS subtitles format, the subtitles are always loaded properly by VLC and also by vlcj libs.

Thanks a lot in advance for all the help provided.

If this question can be distilled down to how to use external SPU files with non-ASCII characters, you can try this:

Suppose you have some filename for your external SPU file, the filename containing non-ASCII characters, let's call this spuFileName ...

Try:

String asciiFileName = new File(spuFileName)
    .toURI()
    .toASCIIString();

Or:

String asciiFileName = new File(spuFileName)
    .toURI()
    .toASCIIString()
    .replaceFirst("file:/", "file:///");

Then use asciiFileName instead when you specify the SPU file for vlcj.

If I remember correctly, LibVLC requires ASCII strings on its API. This problem can also show itself if you try and play a video with a filename that contains non-ASCII characters (vlcj detects this and handles it automatically).

But I'm not sure if this really is your problem as given the partial log you posted it looks like VLC has indeed detected the SPU tracks correctly.

On the other hand, if this suggestion does actually work, vlcj could be changed to handle this case (an external SPU file) automatically.

When actually selecting SPU for display, whether the SPU are in a separate file or contained within the video itself, the only thing that matters is the id of the SPU track. vlcj passes this id directly to the LibVLC API method. The fact that the track description strings are not being encoded directly does not matter.

In earlier versions of VLC, this id was actually the index of the SPU track - so 0, 1, 2, 3 and so on.

With the current version of VLC (this was changed around February 2013, I think this means VLC 2.1+) this was fixed to use the actual SPU track identifiers.

So depending on your version of VLC, if the track identifiers are not working for you try just passing an index instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM