Resize a Video While Maintaining Aspect Ratio in Ruby

Cloudmersive
2 min readAug 23, 2021

--

The aspect ratio of a video can be a crucial element if you want to resize a video while preserving the original message, and in this quick tutorial, we will show you how to do just that by using an API in Ruby. This helpful tool supports a wide variety of input video formats, so there’s a good chance that it will save you a good deal of time.

To begin, we will add the Ruby client to our Gemfile:

gem 'cloudmersive-video-api-client', '~> 2.0', '>= 2.0.5'

Then, we can input the video file and API key into the following code:

# load the gem
require 'cloudmersive-video-api-client'
# setup authorization
CloudmersiveVideoApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
end
api_instance = CloudmersiveVideoApiClient::VideoApi.newopts = {
input_file: File.new('/path/to/inputfile'), # File | Input file to perform the operation on.
file_url: 'file_url_example', # String | Optional; URL of a video file being used for conversion. Use this option for files larger than 2GB.
max_width: 56, # Integer | Optional; Maximum width of the output video, up to the original video width. Defaults to original video width.
max_height: 56, # Integer | Optional; Maximum height of the output video, up to the original video width. Defaults to original video height.
frame_rate: 56, # Integer | Optional; Specify the frame rate of the output video. Defaults to original video frame rate.
quality: 56, # Integer | 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.
}
begin
#Resizes a Video Preserving the Original Aspect Ratio.
result = api_instance.video_resize_video(opts)
p result
rescue CloudmersiveVideoApiClient::ApiError => e
puts "Exception when calling VideoApi->video_resize_video: #{e}"
end

It’s important to note that the function may use more than one API call, depending on the size of the file, and the length of processing time for the operation. To retrieve your API key, head over to the Cloudmersive website to register for a free account; this provides 800 monthly calls across our entire library of APIs.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet