How To Convert a Video to PNG Still Frames in Node.JS

When working with video, it may become necessary to capture still frame images from your content for use as thumbnails, in promotional materials, or for a variety of other reasons. Rather than parsing through the entire video by hand, the following Cloudmersive API will allow you to capture up to 2000 total frames while also dictating your preferred dimensions for the PNG images.

Image for post
Image for post

Our first step is to install our client:

npm install cloudmersive-video-api-client --save

Or, you can add this block to your package.json:

"dependencies": {
"cloudmersive-video-api-client": "^2.0.1"
}

And then, we can call our function, VideoConvertToStillFrames:

var CloudmersiveVideoApiClient = require('cloudmersive-video-api-client');
var defaultClient = CloudmersiveVideoApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveVideoApiClient.VideoApi();var opts = {
'inputFile': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Input file to perform the operation on.
'fileUrl': "fileUrl_example", // String | Optional; URL of a video file being used for conversion. Use this option for files larger than 2GB.
'maxWidth': 56, // Number | Optional; Maximum width of the output video, up to the original video width. Defaults to original video width.
'maxHeight': 56, // Number | Optional; Maximum height of the output video, up to the original video width. Defaults to original video height.
'framesPerSecond': 8.14 // Number | 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.
};
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.videoConvertToStillFrames(opts, callback);

The API Key can be retrieved from the Cloudmersive website at no cost and will provide you with up to 800 call across any of our APIs. With that, you can retrieve the perfect snapshot to represent your video project!

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store