我可以动态制作正在运行的Java Swing应用程序的视频吗?

时间:2010-02-02 21:10:19

标签: java swing video-capture

我有一个Java swing GUI程序,每秒可以渲染1到25帧。 它只有一个窗口,只有一个面板可以完成所有渲染,例如没有其他Swing组件。

我需要能够在运行时生成我的程序的测试运行视频。问题是常规屏幕投射工具(例如我在运行代码之前启动的第三方应用程序)经常会错过我的一些框架,我需要一个准确的视频。

我知道如何使用Robot类来捕获我的Java窗口的屏幕截图,但是当我运行时,我无法将它们保存到磁盘,它会使所有内容减慢太多。有没有办法让我在运行程序的同时使用Robot类(或者其他一些代码)来动态创建窗口的视频?

谢谢!

4 个答案:

答案 0 :(得分:4)

您可以在Java中使用ffmpeg包装器 - Xuggler和内置Java Robot类。以下是Xuggler的示例代码。

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

// First, let's 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();

另一个选项是Screentoaster网站 - 但我要注意它提供的帧速率。

答案 1 :(得分:0)

如果您在Linux中运行程序,则可以利用recordmydesktop。这是我用过帧率和其他控件的更好的录音程序之一。

答案 2 :(得分:0)

每次更新后,您是否无法调整程序以转储窗口内容以及准确的时间戳?然后,如果您需要,请将这些内容发布到电影中。

这将让你完全掌控。

答案 3 :(得分:0)

如果您只想保存视觉更改,可以使用一些屏幕上限软件:

开源截屏封面:http://www.donationcoder.com/Software/Mouser/screenshotcaptor/index.html(或者只使用普通的alt +打印屏幕,每个状态的ctrl v) 开源视频封顶人:http://camstudio.org/