How to Resize a Video in Java
Our goal with implementing these APIs is to improve your reach and flexibility on online media platforms, to better interact with your target audience.
Join the DZone community and get the full member experience.
Join For FreeWith the vast genres of online media platforms, like social media, online video sites, and more, there are more opportunities than ever before to share your products and content globally. With websites like YouTube, Twitter, and Facebook, and apps like Snapchat and Instagram, your organization’s reach can grow exponentially by advertising to or interacting with audiences with varying demographic profiles. Furthermore, making video content that is easily accessible to your users for topics like tutorials, best practices, or news updates creates a user-friendly, passive way for your audience to learn more about you, where classic news articles often require more in-depth reading.
With these sites, however, often the formatting for video sharing changes from one platform to the next. This may not be an issue if your content does not need to maintain a specific aspect ratio to convey your message. However, if you have formatted your video in a specific way to meet the needs of a genre or desired look, resizing your video while preserving its original aspect ratio will allow you to transfer it between platforms without losing quality or aesthetic value.
The following APIs will allow you to perform either of these actions in Java depending on the needs of your video project. Our goal with implementing these APIs is to improve your reach and flexibility on online media platforms, to better interact with your target audience.
The first API we will review is the resizing tool that does not preserve aspect ratio but allows fully customizable dimensions. Both APIs support multiple video file formats including AVI, ASF, FLV, MP4, MPEG/MPG, Matroska/WEBM, 3G2, MKV, M4V, and MOV. The parameters for this specific API include your input file or file URL (recommended for files larger than 2 GB), maximum width and height, frame rate, quality, and file extension. The online mandatory input, however, is your input file.
To start either of these functions, we first need to install the library using Maven or Gradle. Add the following Jitpack reference to your repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then, add this reference to the dependency:
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>
To install with Gradle, add the reference in your root build.gradle at the end of repositories:
xxxxxxxxxx
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Then, add the dependency:
xxxxxxxxxx
dependencies {
implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v3.54'
}
After you have completed the install, you can add the imports to the top of the file 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.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();
}
This will then return a downloadable video file with your new dimensions. To ensure that this and the other API function properly, you will need to verify that:
- Your input file is valid along with nay other input parameters
- Your API Key has been properly inserted into the code block. This can be retrieved at no cost on the Cloudmersive website and will provide access to 800 monthly calls across our library of APIs.
If you need to preserve the aspect ratio for your video project and want to ensure there is no warping or skewing of your material, the next API will fit your needs. While the parameters are the same for this function as in our first API, rather than resizing to a target width and height, the max width and height inputs will instead act as guidelines in which your video will fit. This means that if your video was originally filmed in a widescreen format, it will retain this format even if the size ratio has been decreased.
Like before, we first need to perform our installs and add the imports to the top of the file. Then, we are clear to call the function as shown below:
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 you can easily convert your video between media platforms while retaining your content’s desired look. For both APIs, one API call is used per 10 MB of file or per each additional minute of processing time over 5 minutes, up to a maximum of 25 minutes total processing time. The maximum output file size is 50 GB.
With the completion of this tutorial, you will be able to resize any video to meet the standards of your project. If you have any questions about using these APIs or inquiries concerning other API solutions, you can visit the Cloudmersive website where our team is happy to help with anything you might need.
Opinions expressed by DZone contributors are their own.
Comments