Split a Video into Two Shorter Videos with Java

Cloudmersive
2 min readApr 27, 2022

--

Looking for a quick, simple & useful video processing API? Our video splitting API will cut a video into two separate videos based on a specified start time, and it provides the option to specify a desired length of the second video created in the operation. To implement this API using Java, just follow instructions below to copy & paste the necessary code snippets.

To begin installing the Java SDK with Maven, first add a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Then add one to the dependency in pom.xml:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

Lastly, include the import classes, supply your Cloudmersive API key (obtainable on our website, Home — Cloudmersive APIs), and call the function:

// 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();
}

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.