It is time that "You are being watched. The government has a secret system: a machine that spies on you every hour of every day" - Person of Interest
So I'm gonna tell you how to capture some videos from Webcam with JavaCV.
Lets get started. In-order to capture videos you must include appropriate ffmpeg...jar file to your class path. This can be found in the JavaCPP bundle that you already downloaded. Make sure you only include the suitable library version. That means if you are using x64 verison then used x64 version otherwise go with x86 version.
So all together we need following 4 libraries (Since I'm experiment using x64 here I mentioned latest x64 versions that I used)
- javacv.jar
- javacpp.jar
- opencv-2.4.6.0-windows-x86_64.jar
- ffmpeg-20130915-git-7ac6c63-windows-x86_64.jar
Create a new java class and have following code on that.
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FFmpegFrameRecorder;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.FrameRecorder;
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.cpp.avutil;
import com.googlecode.javacv.cpp.opencv_core;
import java.util.logging.Level;
import java.util.logging.Logger;
class VideoTest {
public static void main(String[] args) {
try {
OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
grabber.start();
opencv_core.IplImage grabbedImage = grabber.grab();
CanvasFrame canvasFrame = new CanvasFrame("Video with JavaCV");
canvasFrame.setCanvasSize(grabbedImage.width(), grabbedImage.height());
grabber.setFrameRate(grabber.getFrameRate());
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("D:/Video/mytestvideo.mp4", grabber.getImageWidth(), grabber.getImageHeight()); // specify your path
recorder.setFormat("mp4");
recorder.setFrameRate(30);
recorder.setVideoBitrate(10 * 1024 * 1024);
recorder.start();
while (canvasFrame.isVisible() && (grabbedImage = grabber.grab()) != null) {
canvasFrame.showImage(grabbedImage);
recorder.record(grabbedImage);
}
recorder.stop();
grabber.stop();
canvasFrame.dispose();
} catch (FrameGrabber.Exception ex) {
Logger.getLogger(VideoTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (FrameRecorder.Exception ex) {
Logger.getLogger(VideoTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
you can see that I have set frame rate, bit rate and format as well. Try with different values and check what is get change. There are also some other parameters as well.
recorder.setVideoQuality(videoQuality);
recorder.setVideoCodec(videoCodec);
recorder.setPixelFormat(pixelFormat);
Try them as well. I'm didn't discuss how to detect face in this post. But don't get upset. It will be surely on next post.
thank you sir.. your code is 100% working.. but i cant record the audio..i need to record video and audio both..can you help me sir? in advance thanks
ReplyDeletebut sir one doubt..while playing video.. video runs very fast.... what to do sir?
ReplyDeletesir the video is recorded in fast forward moode...can you please tell sir why is it so???
ReplyDeleteAnyone who ever wanna know about,How to play the grabbed audio frame from FrameGrabber.
ReplyDeleteOne may look at my blog,Clearly posted what you want,
http://revealedtricks4u.blogspot.in/2015/03/play-audio-sample-grabbed-using-javacv.html
Regards,
Shihab
I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article... porn
ReplyDeleteI’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article... www.sexliga.pl
ReplyDeleteThis site is really a walk-through for all of the data you wanted about it and didn’t know who to ask. Glimpse here, and you’ll undoubtedly discover it. best asian cams
ReplyDelete