如何从LWJGL显示器写入视频文件? [英] How do I write to a video file from an LWJGL Display?

查看:46
本文介绍了如何从LWJGL显示器写入视频文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我学会了如何通过读取GL_FORWARE中的字节缓冲区来截取LWJGL显示的屏幕截图:

public static void takeScreenShot(){
    GL11.glReadBuffer(GL11.GL_FRONT);
    int width = Display.getDisplayMode().getWidth();
    int height = Display.getDisplayMode().getHeight();
    int bpp = 4;
    ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * bpp);
    GL11.glReadPixels(0, 0, width, height, GL11.GL_RGBA, GL11.GL_NSIGNED_BYTE, buffer);
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss");
    Date date = new Date();
    String datetime = dateFormat.format(date);
    File file = new File(screenshot_dir + "\" + datetime + ".png");
    String format = "PNG";
    BufferedImage image = new BufferedImage(width, height, Bufferedmage.TYPE_INT_RGB);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            int i = (x + (width * y)) * bpp;
            int r = buffer.get(i) & 0xFF;
            int g = buffer.get(i + 1) & 0xFF;
            int b = buffer.get(i + 2) & 0xFF;
            image.setRGB(x, height - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | );
        }
    }
    try {
        ImageIO.write(image, format, file);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

我假设我可以保持每秒从前台缓冲区读取大约60次(我知道这会显著降低性能)。然后,我只需将一定数量的帧写入缓冲区,当缓冲区已满时,该缓冲区将被交换到另一个缓冲区。缓冲区满后,可以将其内容追加到文件中。

如何将字节缓冲区格式化为视频中的帧?

谢谢。

推荐答案

您的问题确实很陈旧,但为了完整起见,我还是要回答它。

我的回答是太大,无法提供示例或解释。这就是我将其他人的教程和官方文档链接起来的原因。

  1. 将场景(或其他任何内容)渲染为2D纹理。(http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-14-render-to-texture/)
  2. 使用glGetTexImage(https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glGetTexImage.xhtml)
  3. 检索纹理数据
  4. 下载用于编码MP4(或您想要的任何内容)的Java库,然后一帧接一帧地编码。

以下是一些伪代码:

create framebuffer
enable framebuffer
for all frames {
    render to framebuffer
    glGetTexImage(...)
    library.encodeFrame(imageData)
}

这非常普遍,并且在很大程度上取决于您用于编码的库。

这篇关于如何从LWJGL显示器写入视频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆