title: Capture video using webcam in Java
body: <p>I am trying to capture video screenshot from a java application. I have donwloaded the <strong>sarxos/webcam-capture</strong> library. I have add to my project the executable jar. It is a little bit mess. I want to capture video from a simple javafx interface that I have created. The issue is that after installing the lib and slf4 then it required to install also xuggle. I add xuggle in the path and my code is the following:</p>
  
  <pre><code>File file = new File("output.ts");
  
  IMediaWriter writer = ToolFactory.makeWriter(file.getName());
  Dimension size = WebcamResolution.QVGA.getSize();
  
  writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_H264, size.width, size.height);
  
  Webcam webcam = Webcam.getDefault();
  webcam.setViewSize(size);
  webcam.open(true);
  
  long start = System.currentTimeMillis();
  
  for (int i = 0; i &lt; 50; i++) {
  
      System.out.println("Capture frame " + i);
  
      BufferedImage image = ConverterFactory.convertToType(webcam.getImage(), BufferedImage.TYPE_3BYTE_BGR);
      IConverter converter = ConverterFactory.createConverter(image, IPixelFormat.Type.YUV420P);
  
      IVideoPicture frame = converter.toPicture(image, (System.currentTimeMillis() - start) * 1000);
      frame.setKeyFrame(i == 0);
      frame.setQuality(0);
  
      writer.encodeVideo(0, frame);
  
      // 10 FPS
      Thread.sleep(100);
  }
  
  writer.close();
  
  System.out.println("Video recorded in file: " + file.getAbsolutePath());
  </code></pre>
  
  <p>However I am receiving the following:</p>
  
  <pre class="lang-none prettyprint-override"><code>Caused by: java.lang.UnsatisfiedLinkError: no xuggle-ferry in java.library.path
  </code></pre>
  
  <p><strong>EDIT2</strong> I also tried the jxcapture library. I add in my project all the necessary libs I run the following code: <a href="https://sites.google.com/a/teamdev.com/jxcapture-support/samples/video/windows/capture-video-from-web-camera" rel="nofollow">enter link description here</a>, the code worked fine I manage to store the video, however in the end I am receiving the following error:</p>
  
  <blockquote>
    <p>8614 [JNIWrapper.ShutdownHook] ERROR com.jniwrapper.NativeResourceCollector - 
    com.jniwrapper.FunctionExecutionException: Callback parameter types or their count are not correct
        at com.jniwrapper.Function.invokeVirtualFunc(Native Method)</p>
  </blockquote>
  
  <p>Any idea what this error is about?? What is JNIWrapper.ShutdownHook? I tried the code in 3 different machines and this is happened just to one of them? How can I handle this exception?</p>
  
  <p>EDIT3: I tried @whitesite proposal EDIT3. The gui started and I have two buttons start and stop the start button opened the camera and when I tried to stop I received the following:</p>
  
  <blockquote>
    <p>java.lang.NullPointerException: Pointer address of argument 0 is NULL.
        at org.bytedeco.javacpp.opencv_videoio$VideoWriter.write(Native Method)
        at org.bytedeco.javacv.OpenCVFrameRecorder.record(OpenCVFrameRecorder.java:105)
        at Test1.lambda$0(Test1.java:52)
        at java.lang.Thread.run(Unknown Source)</p>
  </blockquote>