Video Processing API: Cut a Video to a Shorter Length with Java
If you’re looking for a quick, simple way to edit video lengths without opening the video files themselves, you’ve come to the right place. Our video shortening API will cut an input video based on specified start and end times, and it conveniently supports a large variety of video file formats. To use this API yourself, take advantage of code provided in Java below, or give the Cloudmersive API Console a visit to find comparable code snippets available in many other common programming languages.
Let’s begin by installing the Java SDK with Maven (follow the link above to find additional instructions for Gradle). First, add a reference to the pom.xml repository:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Next, add one to the pom.xml dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Finally, include the import classes before calling the video processing 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();
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();
}