Transform a Video into WEBM Format in Ruby

Cloudmersive
2 min readAug 25, 2021

If you are looking to optimize video content to share to your application or various media platforms, the WEBM format is often the best choice, due to its compatibility with HTML5 audio and video. In this quick tutorial, we will show you how to use an API in Ruby to automatically detect the input video format and convert it to the web-friendly WEBM format.

The first step is adding the Ruby client to the Gemfile:

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

After this, you can input the video file and API key into the following code, and optionally specify parameters such as max width and height, preservation of the aspect ratio, frame rate, and quality.

# 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.
preserve_aspect_ratio: true, # BOOLEAN | Optional; If false, the original video's aspect ratio will not be preserved, allowing customization of the aspect ratio using maxWidth and maxHeight, potentially skewing the video. Default is true.
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.
}
begin
#Convert Video to WEBM format.
result = api_instance.video_convert_to_webm(opts)
p result
rescue CloudmersiveVideoApiClient::ApiError => e
puts "Exception when calling VideoApi->video_convert_to_webm: #{e}"
end

And we’re done! To retrieve your API key, visit the Cloudmersive website to register for a free account; this provides 800 calls/month across all of our APIs.

--

--

Cloudmersive

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