gpt4 book ai didi

java - 裁剪 BufferedImage 以在 Xuggle 编码视频中使用

转载 作者:行者123 更新时间:2023-11-30 07:36:20 25 4
gpt4 key购买 nike

我有一个应用程序可以捕获屏幕视频并保存到文件中。我让用户能够选择 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[类型=CODEC_TYPE_VIDEO;id=CODEC_ID_H264;名称=libx264;];时基=1/50;帧速率=0/0;像素类型=YUV420P;宽度=1696;高度=813;];帧速率:0/0 ;时基:1/90000;方向:OUTBOUND;]: 不允许操作

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

抱歉问了这个冗长的问题。我有兴趣了解什么和为什么,而不仅仅是解决方案。因此,如果有人可以解释为什么它不起作用,或者指出我可以提供帮助的 Material ,我将不胜感激。谢谢!

最佳答案

尝试将尺寸设置为偶数 1696x812

关于java - 裁剪 BufferedImage 以在 Xuggle 编码视频中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35349139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com