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 FreeOptimizing 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:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then, we will add a reference to the dependency:
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.90</version>
</dependency>
</dependencies>
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:
xxxxxxxxxx
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.VideoApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
VideoApi apiInstance = new VideoApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
String fileUrl = "fileUrl_example"; // String | Optional; URL of a video file being used for conversion. Use this option for files larger than 2GB.
Integer maxWidth = 56; // Integer | Optional; Maximum width of the output video, up to the original video width. Defaults to original video width.
Integer maxHeight = 56; // Integer | Optional; Maximum height of the output video, up to the original video width. Defaults to original video height.
Integer frameRate = 56; // Integer | Optional; Specify the frame rate of the output video. Defaults to original video frame rate.
Integer quality = 56; // Integer | Optional; Specify the quality of the output video, where 100 is lossless and 1 is the lowest possible quality with highest compression. Default is 50.
String extension = "extension_example"; // String | Optional; Specify the file extension of the input video. This is recommended when inputting a file directly, without a file name. If no file name 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.
try {
byte[] result = apiInstance.videoResizeVideo(inputFile, fileUrl, maxWidth, maxHeight, frameRate, quality, extension);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling VideoApi#videoResizeVideo");
e.printStackTrace();
}
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.
xxxxxxxxxx
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.VideoApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
VideoApi apiInstance = new VideoApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
String fileUrl = "fileUrl_example"; // String | Optional; URL of a video file being used for conversion. Use this option for files larger than 2GB.
Integer maxWidth = 56; // Integer | Optional; Maximum width of the output video, up to the original video width. Defaults to original video width.
Integer maxHeight = 56; // Integer | Optional; Maximum height of the output video, up to the original video width. Defaults to original video height.
Integer frameRate = 56; // Integer | Optional; Specify the frame rate of the output video. Defaults to original video frame rate.
Integer quality = 56; // Integer | Optional; Specify the quality of the output video, where 100 is lossless and 1 is the lowest possible quality with highest compression. Default is 50.
String extension = "extension_example"; // String | Optional; Specify the file extension of the input video. This is recommended when inputting a file directly, without a file name. If no file name 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.
try {
byte[] result = apiInstance.videoResizeVideoSimple(inputFile, fileUrl, maxWidth, maxHeight, frameRate, quality, extension);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling VideoApi#videoResizeVideoSimple");
e.printStackTrace();
}
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.
Comments