代码之家  ›  专栏  ›  技术社区  ›  Warlax

我能即时制作一个运行JavaSwing应用程序的视频吗?

  •  5
  • Warlax  · 技术社区  · 15 年前

    我有一个JavaSwing GUI程序,它在每秒1到25帧之间渲染。 我只对一个窗口和一个面板进行渲染,例如没有其他的swing组件。

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

    我知道如何使用机器人类来捕获我的Java窗口的截图,但是我不可能在我运行时把它们保存到磁盘上,这会减慢太多的内容。有没有办法让我在运行程序的同时,使用robot类(或者其他代码)创建一个动态窗口的视频?

    谢谢!

    4 回复  |  直到 15 年前
        1
  •  4
  •   cetnar    15 年前

    您可以在Java中使用FFMPEG包装器。 Xuggler 构建Java Robot 上课。这是xugler的示例代码。

    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 网站-但我注意到它提供的帧率。

        2
  •  0
  •   TheBuzzSaw    15 年前

    如果在Linux中运行程序,可以利用 recordmydesktop . 这是一个更好的录音程序,我用控制的帧速率和什么都没有。

        3
  •  0
  •   Thorbjørn Ravn Andersen    15 年前

    难道你不能调整你的程序在每次更新后都转储你的窗口内容和一个准确的时间戳吗?如果你需要的话,可以把它们加工成电影。

    这会让你完全控制。

        4
  •  0
  •   sibidiba    15 年前

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

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