Search code examples
javavideoresizebyterescale

How can i resize the video bytes to smaller resolution in java?


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


Solution

  • 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