I have an ImageView which shows Webcam based on Sarxos Webcam library. I display Webcam feed live perfectly.I am trying to capture video from that webcam.
I used Encoder.java example based which is in the folder webcam-capture-master\webcam-capture-examples.
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamResolution;
import com.xuggle.mediatool.IMediaWriter;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.xuggler.ICodec;
import com.xuggle.xuggler.IPixelFormat;
import com.xuggle.xuggler.IVideoPicture;
import com.xuggle.xuggler.video.ConverterFactory;
import com.xuggle.xuggler.video.IConverter;
public class Encoder {
public static void main(String[] args) throws Throwable {
File file = new File("output2.mp4");
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());
}
}
This saves video and shows perfectly.This needs Xuggler library as dependency. I use 5.4 version as .jar it works perfectly.
On my actual code i wanna use webcam capture
protected void startWebCamStream() {
File file = new File("output.mp4");
// -> -> -> -> -> -> -> -> > -> -> -> >
if(file == null) {
System.out.println("error");
exit(2);
}
IMediaWriter writer = ToolFactory.makeWriter(file.getName());
writer.setMaskLateStreamExceptions(true); // KAPATMIYOSA -->
/* vidStop.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
writer.close();
}
});
*/
Dimension size = WebcamResolution.QQVGA.getSize(); // SUNA BAKMAM LAZIM KESIN YANLIS
//Dimension size = new Dimension(190,290);
writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_H264, size.width, size.height);
stopCamera = false;
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
//while(!stopcam)
long start = System.currentTimeMillis();
for(int i=0;!stopCamera;i++) {
try {
if ((grabbedImage = selWebCam.getImage()) != null) {
int finalI = i;
Platform.runLater(new Runnable() {
@Override
public void run() {
BufferedImage image = ConverterFactory.convertToType(selWebCam.getImage(), BufferedImage.TYPE_3BYTE_BGR);
IConverter converter = ConverterFactory.createConverter(image, IPixelFormat.Type.YUV420P);
IVideoPicture frame = converter.toPicture(image,(System.currentTimeMillis() - start) * 1000);
frame.setKeyFrame(finalI == 0);
frame.setQuality(0);
writer.encodeVideo(finalI, frame);
final Image mainiamge = SwingFXUtils
.toFXImage(grabbedImage, null);
imageProperty.set(mainiamge);
// BURAYI SILMEK GEREKIR MI BİLMIYOM
try {
sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
grabbedImage.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
writer.setForceInterleave(false);
// writer.setForceInterleave(false);
return null;
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
imgWebCamCapturedImage.imageProperty().bind(imageProperty);
}
This code is not capturing video as correctly. ( It shows webcam still perfectly.) It saves 1 kb mp4 file which doesnt run.
What is the problem ? My assumptions
-> Size not fitting with actual ImageView Size gave me a lot of errors while trying to compile this code. I changed a lot. If i change Dimensions to another it gives error/warning. -> Runnable method kinda brokes program? Working record program doesnt have its a difference
Please any help is appreciated.