1

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

Srikanth
  • 21
  • 7
  • Can you share what you have tried and what was the output – YUSOF KHAN Aug 10 '20 at 07:28
  • I tried to implement the above logic for video also and failed. ImageIO is not working for video bytes – Srikanth Aug 10 '20 at 07:29
  • So do you want to convert it to smaller bytes and play it in the compiler or do you want to convert it then you will open from your pc – YUSOF KHAN Aug 10 '20 at 07:31
  • Here i am sending the bytes back as response to UI application where they are using it and displaying the video playback. Size of the videos is very high in this scenario.Hence i have to reduce the size and send back the response – Srikanth Aug 10 '20 at 07:35
  • Well then I have made some research try to check this website hope it does help https://www.jochenhebbrecht.be/site/index.php/2010-10-12/java/converting-resizing-videos-in-java-xuggler – YUSOF KHAN Aug 10 '20 at 07:42
  • I have already gone through the link but the comments says it will increase the size whereas my requirement is to reduce the size – Srikanth Aug 10 '20 at 08:03
  • Thanks Guys for the response but i have found the solution and have implemented an artifact based on it so that it can be useful to others. Please check out below – Srikanth Oct 06 '20 at 12:37

1 Answers1

1

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

Srikanth
  • 3
  • 1
Srikanth
  • 21
  • 7