DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • From APIs to Event-Driven Systems: Modern Java Backend Design
  • Jakarta EE Glossary: The Terms Every Java Engineer Should Actually Understand
  • Translating OData Queries to MongoDB in Java With Jamolingo
  • Scaling AI Workloads in Java Without Breaking Your APIs

Trending

  • Architecting an Embedded Efficiency Layer: A Platform Deep Dive into Day-Two Operational Tuning
  • Building Enterprise-Grade Real-Time IoT Dashboards with Vue 3, MQTT, and Kafka
  • Solving the Mystery: Why Java RSS Grows in Docker on M1 Macs
  • Ujorm3: A New Lightweight ORM for JavaBeans and Records
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Resize a Video in Java

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.

By 
Brian O'Neill user avatar
Brian O'Neill
DZone Core CORE ·
Oct. 27, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
8.0K Views

Join the DZone community and get the full member experience.

Join For Free

With 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:

XML
 




x


 
1
<repositories>
2
    <repository>
3
        <id>jitpack.io</id>
4
        <url>https://jitpack.io</url>
5
    </repository>
6
</repositories>



Then, add this reference to the dependency:

XML
 




xxxxxxxxxx
1


 
1
<dependencies>
2
<dependency>
3
    <groupId>com.github.Cloudmersive</groupId>
4
    <artifactId>Cloudmersive.APIClient.Java</artifactId>
5
    <version>v3.54</version>
6
</dependency>
7
</dependencies>



To install with Gradle, add the reference in your root build.gradle at the end of repositories:

Java
 




xxxxxxxxxx
1


 
1
allprojects {
2
    repositories {
3
        ...
4
        maven { url 'https://jitpack.io' }
5
    }
6
}



Then, add the dependency:

Java
 




xxxxxxxxxx
1


 
1
dependencies {
2
        implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v3.54'
3
}



After you have completed the install, you can add the imports to the top of the file and call the function:

Java
 




xxxxxxxxxx
1
30


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.VideoApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
VideoApi apiInstance = new VideoApi();
17
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
18
String fileUrl = "fileUrl_example"; // String | Optional; URL of a video file being used for conversion. Use this option for files larger than 2GB.
19
Integer maxWidth = 56; // Integer | Optional; Maximum width of the output video, up to the original video width. Defaults to original video width.
20
Integer maxHeight = 56; // Integer | Optional; Maximum height of the output video, up to the original video width. Defaults to original video height.
21
Integer frameRate = 56; // Integer | Optional; Specify the frame rate of the output video. Defaults to original video frame rate.
22
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.
23
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.
24
try {
25
    byte[] result = apiInstance.videoResizeVideoSimple(inputFile, fileUrl, maxWidth, maxHeight, frameRate, quality, extension);
26
    System.out.println(result);
27
} catch (ApiException e) {
28
    System.err.println("Exception when calling VideoApi#videoResizeVideoSimple");
29
    e.printStackTrace();
30
}



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:

Java
 




xxxxxxxxxx
1
31


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.VideoApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
VideoApi apiInstance = new VideoApi();
17
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
18
String fileUrl = "fileUrl_example"; // String | Optional; URL of a video file being used for conversion. Use this option for files larger than 2GB.
19
Integer maxWidth = 56; // Integer | Optional; Maximum width of the output video, up to the original video width. Defaults to original video width.
20
Integer maxHeight = 56; // Integer | Optional; Maximum height of the output video, up to the original video width. Defaults to original video height.
21
Integer frameRate = 56; // Integer | Optional; Specify the frame rate of the output video. Defaults to original video frame rate.
22
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.
23
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.
24
try {
25
    byte[] result = apiInstance.videoResizeVideo(inputFile, fileUrl, maxWidth, maxHeight, frameRate, quality, extension);
26
    System.out.println(result);
27
} catch (ApiException e) {
28
    System.err.println("Exception when calling VideoApi#videoResizeVideo");
29
    e.printStackTrace();
30
}
31

          



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.

Java (programming language) API

Opinions expressed by DZone contributors are their own.

Related

  • From APIs to Event-Driven Systems: Modern Java Backend Design
  • Jakarta EE Glossary: The Terms Every Java Engineer Should Actually Understand
  • Translating OData Queries to MongoDB in Java With Jamolingo
  • Scaling AI Workloads in Java Without Breaking Your APIs

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook