Resize a Video in Java
Learn how to resize a video with the optional preservation of the aspect ratio using APIs in Java.
Join the DZone community and get the full member experience.Join For Free
Optimizing your brand’s presence can be a difficult task when your audience is scattered across current and emerging online media platforms. One way to achieve your goals and establish brand awareness is to share video content. This can be an engaging approach to share visuals, communicate information, and reach a diverse audience. Unfortunately, with size requirements differing from one platform to the next, managing the formatting of videos can get a bit challenging.
If resizing a video is necessary, it is important to determine whether you prefer to maintain the current aspect ratio or customize it to fit the new parameters. To clarify, the aspect ratio is the ratio of an image’s width to its height, and it influences how a resized video looks in comparison with the original vision. If you want to alter the video size, but don’t want to change the way that it’s viewed, it is necessary to preserve the aspect ratio. On the other hand, you may be willing to flex a bit on your dimensions to fit a new format.
The following APIs will provide you with two automated options for resizing your video in Java; one will maintain your original aspect ratio, and the other will allow you to input new customizable dimensions. Both functions support several input video formats, including AVI, FLV, MP4, MOV, and more. They use 1 API call per 10 MB of file size, and 1 API call per additional minute of processing time over 5 minutes, up to a maximum of 25 minutes total processing time. The maximum output file size is 50GB.
To begin, we will install the Maven SDK by adding a reference to the jitpack repository in pom.xml:
Then, we will add a reference to the dependency:
With the installation complete, we can discuss our first API. The function below will resize a video while maintaining the original aspect ratio and encoding, which will allow you to preserve the original vision and convey the content in the style of your choosing. To perform the operation, we need to add the imports to the top of the controller and call the function:
Now if you prefer a more customizable editing experience, you can use the following function to resize a video without preserving the original aspect ratio; this will allow you to fit any video format by making slight alterations. However, keep in mind when choosing this approach that it does increase the potential for image skewing.
To ensure that both resize functions run smoothly, you will need to fulfill a couple of required parameters, and have the option to input a few others:
- Input File (required): the specific video file you wish to perform the operation on.
- API Key (required): your personal API key; if you don’t already have one, it can be retrieved by registering for a free account on the Cloudmersive website.
- File URL (optional): the URL of the video you file you wish to perform the operation on; this is recommended for any file over 2GB.
- Max Width (optional): maximum width of the output video, up to the original video width; defaults to original video width.
- Max Height (optional): maximum height of the output video, up to the original video width; defaults to original video height.
- Frame Rate (optional): frame rate of the output video; defaults to original video frame rate.
- Quality (optional): quality of the output video, where 100 is lossless and 1 is the lowest possible quality with the highest compression; default is 50.
- Extension (optional): file extension of the input video; this is recommended when inputting a file directly, without a file name. If no filename is available and no extension is provided, the extension will be inferred from the file data, which may cause a different extension to be used in the output.
Whether you choose to maintain or adjust the aspect ratio, we hope this tutorial fits your video resizing needs.
If you want to check out more video APIs, you can find them and many other functions over on the Cloudmersive website. Or, if you have questions, feel free to reach out to our team; they’re always happy to assist.
Opinions expressed by DZone contributors are their own.