Search code examples
javavlclibvlcsubtitlevlcj

Validate subtitles file using vlcj (Java)


I am using an instance of vlcj DefaultEmbeddedMediaPlayer to play video. When adding subtitles to the video I want to see if the subtitle file is valid, and throw an Exception if it is not. This is my code:

File subtitlesFile = new File("subs.srt");
player.setSubTitleFile(subTitleFile);

The method setSubTitleFile(File subTitleFile) doesn't have a return value, but looking into its implementation I can see that it passes the call to libvlc, calling libvlc_video_subtitle_file(libvlc_media_player_t p_mi, String psz_subtitle), which returns 1 or 0 depending on if the subtitles have been added successfully or not. So I inherited from DefaultEmbeddedMediaPlayer and wrote my own version of that method:

public void setSubTitleFile(String subTitleFileName) {
    final int status = libvlc.libvlc_video_set_subtitle_file(mediaPlayerInstance(), subTitleFileName);
    System.out.println(status);
    // throw Exception if status is false
}

Surprisingly this always gives me status 1, which means success. Even if I am passing an empty text file. Is this a bug? I really do not want to parse the subtitle file manually beforehand. Using the native VLC Player, I am getting an error as expected if the subtitles are invalid. Anything I could do here?

EDIT: This is a screenshot from the native VLC player when adding an empty SRT file during play. VLC subtitles file error


Solution

  • As far as I know, LibVLC/VLC is not parsing the subtitle file when you call libvlc_video_set_subtitle_file. All it is basically doing is setting a string value on some native structure that will be used later. So, you get a status of "success" because that call did its job of setting that string value.

    Therefore, you will only know if the subtitle file is valid when VLC starts playing your media and it attempts to load the file at that point. Even then, I do not think you will get a specific error message - there will simply be an absence of a subtitle track.