Convert a Video to Still Frame PNGs in Java
Creating the perfect thumbnail image is imperative to effectively drawing an audience to your video content. We have previously shown you an API that will allow you to instantly convert video files to still-frame PNGs, letting you to take single frame captures with ease. In this article, we will be showing you how to perform this function using Java. To run the function, you will need to input a valid video file or URL. You can also choose to set a maximum width and height as well as dictate the number of frames per second to be returned as PNG stills. This API has support for many input video formats including AVI, ASF, FLV, MP4, MPEG/MPG, Matroska/WEBM, 3G2, OGV, MKV, M4V and MOV.
Our first step will be to install the SDK by adding a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then, we will add a reference to the dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>
Now, we can call our 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.
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.
BigDecimal framesPerSecond = new BigDecimal(); // BigDecimal | Optional; How many video frames per second to be returned as PNG images. Minimum value is 0.1, maximum is 60. Default is 1 frame per second. Maximum of 2000 total frames.
try {
StillFramesResult result = apiInstance.videoConvertToStillFrames(inputFile, fileUrl, maxWidth, maxHeight, framesPerSecond);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling VideoApi#videoConvertToStillFrames");
e.printStackTrace();
}
With this, you can automatically capture the perfect still frame to represent your video content. You can retrieve your free API Key from Cloudmersive. This will give you access to 800 monthly calls across our library of APIs.