如何在Java中将视频字节调整为较小的分辨率?

huangapple go评论83阅读模式
英文:

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

&lt;dependency&gt;
&lt;groupId&gt;io.github.techgnious&lt;/groupId&gt;
&lt;artifactId&gt;IVCompressor&lt;/artifactId&gt;
&lt;version&gt;1.0.1&lt;/version&gt;
&lt;/dependency&gt;

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

huangapple
  • 本文由 发表于 2020年8月10日 15:25:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63335797.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定