How to Resize a Video while Preserving Aspect Ratio in Node.JS

Maintaining the correct aspect ratio for a video is extremely important to convey your content in the way that you choose. When resizing a video, you may run into a problem with your aspect ratio changing to scale with the new size. Using this Cloudmersive API in Node.JS, however, you can preserve the aspect ratio while altering the size of a video to fit your needs.

Image for post
Image for post

First, you’ll need to install our client software:

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

Or, add this snippet to your package.json:

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

Next, you can call the function, VideoResizeVideo:

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.
'frameRate': 56, // Number | Optional; Specify the frame rate of the output video. Defaults to original video frame rate.
'quality': 56, // Number | Optional; Specify the quality of the output video, where 100 is lossless and 1 is the lowest possible quality with highest compression. Default is 50.
'extension': "extension_example" // String | Optional; Specify the file extension of the input video. This is recommended when inputting a file directly, without a file name. If no file name is available and no extension is provided, the extension will be inferred from the file data, which may cause a different extension to be used in the output.
};
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.videoResizeVideo(opts, callback);

With that, all of your future video projects will look exactly the way you planned. To receive you personal API Key, giving you access to up to 800 calls across our entire API library, visit the Cloudmersive website.

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