I am trying to capture video screenshot from a java application. I have donwloaded the sarxos/webcam-capture 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:
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 < 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());
However I am receiving the following:
Caused by: java.lang.UnsatisfiedLinkError: no xuggle-ferry in java.library.path
EDIT2 I also tried the jxcapture library. I add in my project all the necessary libs I run the following code: enter link description here, the code worked fine I manage to store the video, however in the end I am receiving the following error:
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)
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?
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:
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)
Ok, that's what I got using this JavaCV library and their examples:
It captures video from web cam and translates it live and in the same time writes to file. You need to include their library in classpath using maven or manual installation and also all the jars from binary zip archive. Maybe you don't need all of them, but I did't dig deep.
You can play with settings and investigate examples and sources. From what I've seen it's pretty powerful library.
Edit
Just video processing part:
You need to start process
videoProcessor.start()
somewhere and then interrupt it to stop recordingvideoProcessor.interrupt()
. You might also need to set proper recorder frame rate to adjust it to your camera fps.Edit 2
Code to output available devices:
Edit 3
If webcam-capture works, than you can use it to capture images and use javaCV to record these images into videofile (pretty much like exapmle in youe question, just javaCV instead of xuggler). This is a small javafx app, which has two buttons: 'Start' starts the videoProcessing thread to capture video and 'Stop' interrupts this thread:
You are missing slf4-api-ver.jar in your classpath. However adding just the api will not be enough you will also need a provider like slf4j-simple-ver.jar. The latest available are version 1.7.21 and can be downloaded from here http://www.slf4j.org/download.html