英文:
How can i resize the video bytes to smaller resolution in java?
问题
我目前正在使用REST API。我一直在尝试通过Java将接收到的视频字节调整为较小的分辨率。我可以使用Java中的BufferedImage和ImageIO来调整图像大小,但是对于视频字节却无法实现相同的操作。
以下是用于图像调整大小的代码。请建议如何调整视频字节的大小。
private ByteArrayOutputStream resizeImage(byte[] x) throws IOException {
BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(x));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
if (x.length / 1024 > 500) {
int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
BufferedImage resizedImage = new BufferedImage(1280, 720, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, 1280, 720, null);
g.dispose();
ImageIO.write(resizedImage, "jpeg", outputStream);
} else {
ImageIO.write(originalImage, "jpeg", outputStream);
}
return outputStream;
}
我需要一些类似的逻辑来处理视频,以便我可以将经过压缩或较小分辨率处理的视频字节流作为响应发送回去。
英文:
I am currently using REST API. I have been trying to resize the video bytes received to me to a smaller resolution through java. I am able to resize the image using BufferedImage and ImageIO in java but am not able to do the same for video bytes.
Here is the code for image resizing. Please suggest how we can resize video bytes
private ByteArrayOutputStream resizeImage(byte[] x) throws IOException {
BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(x));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
if (x.length / 1024 > 500) {
int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
BufferedImage resizedImage = new BufferedImage(1280, 720, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, 1280, 720, null);
g.dispose();
ImageIO.write(resizedImage, "jpeg", outputStream);
} else {
ImageIO.write(originalImage, "jpeg", outputStream);
}
return outputStream;
}
I need some similar logic for video as well so that i can send back the compressed or smaller resolution video bytestream back as response
答案1
得分: 1
我已经以某种方式找到了解决我的问题的方法。我利用了一些工具并构建了一个自定义项目来处理这些问题。
这个工具可以用来将图像和视频调整为较低分辨率,质量非常好。
项目依赖的URL(适用于Maven、Gradle、Scala等...):点击这里
或者您可以将以下依赖项添加到pom.xml中:
<dependency>
<groupId>io.github.techgnious</groupId>
<artifactId>IVCompressor</artifactId>
<version>1.0.1</version>
</dependency>
用法:
IVCompressor compress = new IVCompressor();
byte[] Imageoutput = compress.resizeImage(data, fileFormat, resolution); //用于图像压缩
byte[] VideoOutput = compress.reduceVideoSize(videoData, fileFormat, resolution); //用于视频压缩
它可以将25MB的视频转换为300-400KB,默认设置下。它还有许多其他带有自定义设置的方法。请查看此页面IVCompressor以获取更多信息。谢谢
英文:
I have somehow found the solution to my problem. I have utilized few tools and built a custom project to handle such issues.
This artifact can be used to resize images as well as videos to lower resolution with very good quality
URL for Project dependency(Available for Maven, Gradle, Scala etc..): Click Here
Or you can add below dependency to pom.xml
<dependency>
<groupId>io.github.techgnious</groupId>
<artifactId>IVCompressor</artifactId>
<version>1.0.1</version>
</dependency>
Usage::
IVCompressor compress=new IVCompressor();
byte[] Imageoutput=compress.resizeImage(data, fileFormat, resolution); //for image compression
byte[] VideoOutput=compress.reduceVideoSize(videoData, fileFormat, resolution);//for video compression
It can convert 25MB video into 300-400kb with default settings. It have many other methods with custom settings. Do check out this page IVCompressor for more info. Thank you
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论