5

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

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

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

谢谢!

4

4 回答 4

4

您可以在 Java - Xuggler和内置 Java Robot类中使用 ffmpeg 包装器。这是 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网站 - 但我注意到它提供的帧速率。

于 2010-02-02T22:58:47.097 回答
0

如果您在 Linux 中运行程序,则可以利用recordmydesktop。它是我用过的更好的录制程序之一,可以控制帧率等等。

于 2010-02-02T21:19:41.997 回答
0

您不能调整您的程序以在每次更新后转储窗口的内容以及准确的时间戳吗?如果需要,然后将这些处理成电影。

这将使您完全控制。

于 2010-02-02T21:41:22.480 回答
0

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

开源截图封盖器:http: //www.donationcoder.com/Software/Mouser/screenshotcaptor/index.html(或者只使用普通的alt+print屏幕,每个状态都按ctrl v)开源视频封盖器:http://camstudio .org/

于 2010-02-02T22:46:44.127 回答