How To Implement Video Information and Editing APIs in Java
In the past 15+ years, online video traffic has experienced a dramatic boom utterly unmatched by any other form of content. According to data provided by Sandvine in their 2022 Global Internet Phenomena Report, video traffic accounted for 53.72% of the total volume of internet traffic in 2021, and the closest trailing category (social) came in at just 12.69%. As this report goes on to highlight, video traffic has become so pervasive that it’s now directly influencing traffic in other measurable content categories (such as on messaging, gaming, and social) where video content is frequently viewed and disseminated.
It must be said that this video traffic phenomenon primarily owes itself to modernizations in the scalability of streaming infrastructure, which simply weren’t present fifteen years ago. A proportionate level of demand for video content was (probably) always there, but the willingness to pause videos and sit through long, frustrating video buffer times certainly put a dent in that demand. Not only has near-infinitely scalable cloud storage reduced the burden of storing large video files, but CDNs (content delivery networks) deployed by video streaming and social media giants in this timeframe have all but eliminated those slow server-to-client buffering times, which initially plagued the user experience.
Of course, not every website can afford to spend billions of dollars establishing global CDNs for streaming, and newly developed websites with aspiring video capabilities can’t hope to keep up with large independent providers in their pursuit of streaming bandwidth. A simple fact has become clear, though, which cannot be ignored: internet patrons are increasingly growing accustomed to a video-centric online experience. We, as consumers, are the ones responsible for bringing the volume of video traffic to its incredible height, and the infrastructural innovations making that possible have merely followed in support of our insatiable demand. We stream and record meetings, capture handheld clips of our pets, and engage constantly with others online who do the same. Video interaction is something we increasingly expect as part of our online experience.
As a result of this growth in video traffic, web apps developed with media upload workflows—regardless of industry vertical—increasingly need to incorporate video-centric features to keep up with consumer expectations. While raw image uploads only need to be cropped, filtered, or overlayed with text, video uploads need to be clipped and edited to specific lengths before finding a home online. Implementing video editing features such as these is no small task; however, video processing, let alone storage, can be extremely burdensome for a growing web application’s infrastructure to handle on its own. Thankfully, video editing web APIs can make a big difference: they can reduce the volume of code required for an application to process videos and offload much of the heavy lifting to external servers.
Demonstration
The purpose of this article is to demonstrate three separate API solutions that can positively impact the video upload and editing process on a website. Each API supports a variety of common video formats, including MP4, MPG, AVI, MOV, and more. To help with the implementation process, ready-to-run Java code examples have been provided with additional documentation further down the page. These APIs perform the three following operations:
- Get Detailed Information About a Video (or Audio) File
- Cut a Video to a Shorter Length
- Split a Video Into Two Shorter Videos
Each API can be used for free with a free-tier API key (this can be obtained on our website). To install the SDK, you can follow the below instructions.
To install with Maven, add a reference to the repository in pom.xml
:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
After that, add a reference to the dependency in pom.xml
:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
To install with Gradle, add it in your root build.gradle
at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
After that, add the dependency in build.gradle
:
dependencies {
implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v4.25'
}
1. Get Detailed Information About a Video (or Audio) File
The purpose of calling this API is, as the title suggests, to return detailed information about video uploads (this will work for audio files as well). The information returned includes format, dimensions, file size, bit rate, duration, and start time. All of which is useful to consider in downstream video-editing operations. When structuring requests for this API, either the input file path or file URL can be used (the latter is recommended for files exceeding 2GB in size). The below code examples can be used to structure your API call:
// 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.
try {
MediaInformation result = apiInstance.videoGetInfo(inputFile, fileUrl);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling VideoApi#videoGetInfo");
e.printStackTrace();
}
For your reference, I’ve included an example JSON response body:
{
"Successful": true,
"FileFormat": "string",
"FileFormatFull": "string",
"ValidFileFormats": [
"string"
],
"Width": 0,
"Height": 0,
"Size": 0,
"BitRate": 0,
"Duration": 0,
"StartTime": 0
}
2. Cut a Video to a Shorter Length
Raw video uploads usually aren’t intended to be a final product, so it’s important to allow uploaders to shorten their videos with ease. This API will allow uploaders to cut videos to their desired length by providing a specific start time and subsequently defining how much time should elapse following that start time (both values need to be supplied in TimeSpan format). Files can be uploaded via file path or file URL (the latter option is recommended for files exceeding 2 GB in size), and the API response will return the encoding for the newly created video file.
Since videos vary greatly in size, it’s important to note that this API leverages one API call per 10 MB of file size, and it uses one additional API call per minute of processing time exceeding five minutes (up to a maximum of twenty-five minutes of total processing time). Further, there is a maximum output file size limit of 50 GB.
To call this API, use the following code examples to structure your request:
// 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.
OffsetDateTime startTime = OffsetDateTime.now(); // OffsetDateTime | Optional; Specify the desired starting time of the cut video in TimeSpan format.
OffsetDateTime timeSpan = OffsetDateTime.now(); // OffsetDateTime | Optional; Specify the desired length of the cut video in TimeSpan format. Leave blank to include the rest of the video. Maximum time is 4 hours.
try {
byte[] result = apiInstance.videoCutVideo(inputFile, fileUrl, startTime, timeSpan);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling VideoApi#videoCutVideo");
e.printStackTrace();
}
3. Split a Video Into Two Shorter Videos
This operation differs slightly from the previously described “Cut a Video to a Shorter Length” operation, allowing uploaders to split videos at a specific time and subsequently define the length of the second video. This will return the encoding for two separate new video files rather than one.
Consistent with the previously outlined solution, videos can be included in the request via file path or file URL (the latter option is again recommended for files exceeding 2GB), and the split time and time span (for the second video) must be defined in TimeSpan format.
To call this API, use the following code examples:
// 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();
OffsetDateTime splitTime = OffsetDateTime.now(); // OffsetDateTime | Specify the desired time at which to split the video in TimeSpan format.
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.
OffsetDateTime timeSpan = OffsetDateTime.now(); // OffsetDateTime | Optional; Specify the desired length of the second video in TimeSpan format. Leave blank to include the rest of the video. Maximum time is 4 hours.
try {
SplitVideoResult result = apiInstance.videoSplitVideo(splitTime, inputFile, fileUrl, timeSpan);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling VideoApi#videoSplitVideo");
e.printStackTrace();
}
For your reference, I’ve included an example JSON response body showing how the results of this operation are returned and organized:
{
"Successful": true,
"Videos": [
{
"VideoNumber": 0,
"Content": "string"
}
]
}
Conclusion
With a few simple, intuitive Video APIs in your arsenal, you won’t need to spend any more time worrying about scaling your website’s media upload services—you can get back to focusing on the bigger picture.