2010-02-02 92 views
5

我有一个Java swing GUI程序,可以呈现每秒1到25帧之间的任何地方。 它只有一个窗口,我只做一个面板来完成所有的渲染,例如没有其他Swing组件。我可以制作正在运行的Java Swing应用程序的视频吗?

我需要能够在运行程序时生成测试运行的视频。问题在于,普通的屏幕转换工具(例如,在运行我的代码之前启动的第三方应用程序)往往会错过我的一些框架,我需要一个精确的视频。

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

谢谢!

回答

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网站 - 但我记下确定它提供的帧率。

0

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

+0

对不起,在Windows下运行 - 不喜欢它非常:) – Warlax 2010-02-02 22:24:00

0

难道你不适应你的程序转储你的窗口的内容后,每次更新与准确的时间戳?然后将这些过程发布到电影中,如果你需要的话。

这会给你完全的控制权。

+0

Thorbjørn,谢谢你的快速回复。 我已经这么做了,但是为每一帧保存文件都很慢。 – Warlax 2010-02-02 22:23:44

+0

它看起来像JMF支持编码几种视频格式。可能是要保证你的视频是准确的。 http://java.sun.com/javase/technologies/desktop/media/jmf/2.1.1/formats.html – 2010-02-02 22:55:24

相关问题