How to Convert a Video to an MP4 File in Ruby
When it comes to video production, MP4 files are the ideal format; they include complex audio capabilities, as well as the capacity for subtitles. If you created or downloaded a video in a less accommodating format, you may need to convert the files to MP4 before they can be edited and/or included in your online content. The following API will allow you to automatically convert a wide variety of video formats to MP4, with no additional code-writing needed by you.
We will kick things off by adding the API client to our Gemfile:
gem 'cloudmersive-video-api-client', '~> 2.0', '>= 2.0.5'
Then we can call the function with the below 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'
endapi_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 MP4 format.
result = api_instance.video_convert_to_mp4(opts)
p result
rescue CloudmersiveVideoApiClient::ApiError => e
puts "Exception when calling VideoApi->video_convert_to_mp4: #{e}"
end
To ensure the operation runs smoothly, you will need your video file and API key; there several optional parameters as well including, quality level, frame rate, aspect ratio, and more. If you need to retrieve your API key, head to the Cloudmersive website to register for a free account; this provides 800 monthly calls across our library of APIs.