Repository
https://github.com/jcodec/jcodec
What Will I Learn?
Requirements
Resources for Java and this tutorial:
Difficulty
Tutorial Duration 25- 30 Minutes
TUTORIAL CONTENT
In this Tutorial, we are going to learn how to create a simple program to implement screen recording with Java programming language. To achieve this functionality, we are going to make use of JCodec Library.
What is JCodec ?
JCodec is a pure java implementation of video/audio codecs. JCodec is a library implementing a set of popular video and audio codecs. source. To get Started, we are going to download the libraries then dive straight into NetBeans or Eclipes, depending on your preference.
STEP 1 : Download JCodec libraries
Download the two libraries (.JAR) found on JCodec site or alternatively from the direct links below
STEP 2 : Create a New Java Project
After Creating a new project (with your preferred project name) in Java, you should have something like this;
STEP 3 : Add the JAR files to your project
To add these JAR files to our project, we will follow these steps
STEP 4 : Capture PC Screen
To capture the PC Screen, we are going to use the Java Robot class. Add the code block below to to your main method of your class
public static void main(String[] args) throws AWTException, IOException {
List<BufferedImage> imageList = new ArrayList<>();
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
Robot robot = new Robot();
File file = new File("outputVideo.mp4");
System.out.println("getting screen images...");
int count = 0;
while (count < 100) {
BufferedImage image = robot.createScreenCapture(screenRect);
imageList.add(image);
count++;
}
makeVideoFromImages(imageList,file);
}
Code explanation
STEP 5 : Generate a Video File From The Images(Frames)
At this step, what we will do is take the lists of frames and encode it into the video file we created in Step 4 ."outputVideo.mp4"
Now our imported libraries becomes useful. Lets create a method that handles the conversion. See the code block below;
public static void makeVideoFromImages(List<BufferedImage> imageList, File file) throws IOException {
AWTSequenceEncoder sequenceEncoder = AWTSequenceEncoder.createSequenceEncoder(file, 25);
for (int i = 0; i < imageList.size(); i++) {
sequenceEncoder.encodeImage(imageList.get(i));
System.out.println("list encode " + i);
}
sequenceEncoder.finish();
}
Code explanation
STEP 6 : Test The Java Program
With Steps 1 to 5 completed, our screen recording program is now ready for use. Below is a video showing the program output.
Proof of Work
The complete source code can be found on gitHub