Can I make a video running a Java Swing application on the fly?

I have a GUI program for Java swing that displays from 1 to 25 frames per second. This is only one window and only one panel, to which I am doing all the rendering, for example. no other Swing components.

I need to be able to create test run videos of my program as it launches. The problem is that the usual tools for creating a screen (for example, third-party applications that I run before running my code) often miss some of my frames, and I need an exact video.

I know how to use the Robot class to capture screenshots of my Java window, but I cannot save them to disk at startup, it will slow down too much. Is there a way to use the Robot class (or maybe some other piece of code) to create a video of my window “on the fly” when starting my program?

Thank!

+5
source share
4 answers

You can use the ffmpeg wrapper in Java - Xuggler and the built-in Java Robot . Here is sample code with Xuggler.

final Robot robot = new Robot();
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());

// First, let make a IMediaWriter to write the file.
final IMediaWriter writer = ToolFactory.makeWriter("output.mp4");

// We tell it we're going to add one video stream, with id 0,
// at position 0, and that it will have a fixed frame rate of
// FRAME_RATE.
writer.addVideoStream(0, 0,
 FRAME_RATE,
 screenBounds.width, screenBounds.height);

// Now, we're going to loop
long startTime = System.nanoTime();
for (int index = 0; index < SECONDS_TO_RUN_FOR*FRAME_RATE.getDouble(); index++)
{
    // take the screen shot
   BufferedImage screen = robot.createScreenCapture(screenBounds);

   // convert to the right image type
   BufferedImage bgrScreen = convertToType(screen,
   BufferedImage.TYPE_3BYTE_BGR);

   // encode the image to stream #0
   writer.encodeVideo(0,bgrScreen,
   System.nanoTime()-startTime, TimeUnit.NANOSECONDS);
   System.out.println("encoded image: " +index);

   // sleep for framerate milliseconds
  Thread.sleep((long) (1000 / FRAME_RATE.getDouble()));
}
// Finally we tell the writer to close and write the trailer if
// needed
writer.close();

Another option is the Screentoaster website, but I have no doubt what frame rate it provides.

+4
source

Linux, recordmydesktop. , -.

0

? , .

.

0

All Articles