我有一个捕获屏幕视频并将其保存到文件的应用程序。我为用户提供了在480、720和“全屏”视频尺寸之间进行选择的能力。 480将记录在屏幕上的一个小盒子中,720将记录在一个较大的盒子中,当然,“全屏”将记录在一个更大的盒子中。但是,此全屏框不是实际的屏幕分辨率。它是应用程序窗口的大小,恰好在1700x800左右。视频工具可完美地用于480和720选项,如果“全屏”被覆盖为1920x1080的整个屏幕,则视频工具也将正常工作。

我的问题:只允许某些尺寸吗?它是否必须适合某个长宽比,或者是“可接受的”分辨率?下面的代码是从xuggle CaptureScreenToFile.java文件修改的(问题的位置通过注释指出):

    public void run() {
        try {
            String parent = "Videos";
            String outFile = parent + "example" + ".mp4";
            file = new File(outFile);

            // This is the robot for taking a snapshot of the screen.  It's part of Java AWT
            final Robot robot = new Robot();
            final Rectangle customResolution = where;  //defined resolution (custom record size - in this case, 1696x813)

            final Toolkit toolkit = Toolkit.getDefaultToolkit();
            final Rectangle fullResolution = new Rectangle(toolkit.getScreenSize());  //full resolution (1920x1080)

            // First, let's make a IMediaWriter to write the file.
            final IMediaWriter writer = ToolFactory.makeWriter(outFile);

            writer.setForceInterleave(false);

            // 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, customResolution.width, customResolution.height);  //if I use fullResolution, it works just fine - but captures more of the screen than I want.

            // Now, we're going to loop
            long startTime = System.nanoTime();
            while (recording) {
                // take the screen shot
                BufferedImage screen = robot.createScreenCapture(fullResolution);  //tried capturing using customResolution, but did not work.  Instead, this captures full screen, then tries to trim it below (also does not work).
                // convert to the right image type
                BufferedImage bgrScreen = convertToType(screen, BufferedImage.TYPE_3BYTE_BGR);  //Do I need to convert after trimming?

                BufferedImage trimmedScreen = bgrScreen.getSubimage((int)customResolution.getX(), (int)customResolution.getY(), (int)customResolution.getWidth(), (int)customResolution.getHeight());

                // encode the image
                try{
                    //~~~~Problem is this line of code!~~~~  Error noted below.
                    writer.encodeVideo(0, trimmedScreen, System.nanoTime() - startTime, TimeUnit.NANOSECONDS);  //tried using trimmedScreen and bgrScreen
                } catch (Exception e) {
                    e.printStackTrace();
                }

                // 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();
        } catch (Throwable e) {
            System.err.println("an error occurred: " + e.getMessage());
        }
    }

    public static BufferedImage convertToType(BufferedImage sourceImage, int targetType) {
        BufferedImage image;

        // if the source image is already the target type, return the source image
        if (sourceImage.getType() == targetType)
            image = sourceImage;
        // otherwise create a new image of the target type and draw the new image
        else {
            image = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), targetType);
            image.getGraphics().drawImage(sourceImage, 0, 0, null);
        }

        return image;
    }


错误:
java.lang.RuntimeException:无法打开流com.xuggle.xuggler.IStream@2834912 [index:0; id:0; streamcoder:com.xuggle.xuggler.IStreamCoder@2992432 [codec = com.xuggle.xuggler.ICodec @ 2930320 [type = CODEC_TYPE_VIDEO; id = CODEC_ID_H264; name = libx264;];时基= 1/50;帧频== 0/0;像素类型= YUV420P;宽度= 1696;高度= 813;];帧频:0/0 ; timebase:1/90000; direction:OUTBOUND;]:不允许操作

注意:该文件已成功创建,但大小为零,并且无法由Windows Media Player打开,并显示以下错误文本:
Windows Media Player无法播放文件。播放器可能不支持文件类型,或者可能不支持用于压缩文件的编解码器。

对不起罗y的问题。我对学习“什么”和“为什么”感兴趣,而不仅仅是解决方案。因此,如果有人可以解释为什么它不起作用,或者向我指出可以提供帮助的材料,我将不胜感激。谢谢!

最佳答案

尝试使尺寸为偶数1696x812

10-01 09:46