Search code examples
javavlcj

vlcj webcam stream in java


I am trying to make a simple program that will live stream from my webcam.

public static void main(String[] args) throws Exception
{
    int port = nextAvailable();
    //String media = "/root/Desktop/525600.mp4";
    String media = "/dev/video0";
    String[] options = {":sout=#duplicate{dst=rtp{sdp=rtsp://:"+port+"/stream},dst=display}", ":sout-all", ":sout-keep"};
    MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(args);
    HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();
    mediaPlayer.playMedia(media,options);
    System.out.println("Using port: "+port);
    Thread.currentThread().join();
}

if I use the commented media (/root/Desktop/525600.mp4), the stream works without any issues. However, I do not know how to stream from the webcam. I tried /dev/video0 but it gives the following errors:

[00007fae70008f78] core access error: read error: Invalid argument

[00007fae70008f78] filesystem access error: read error: Invalid argument

[00007fae7000d3d8] core stream error: cannot pre fill buffer

What am I doing wrong?


Solution

  • Simply replaced

    String media = "/dev/video0";
    

    with

    String media = "v4l2:///dev/video0";
    

    and it works now.