Want to stream media using Ruby in the cloud? Azure Media Services provides a handy solution for doing so. With Ruby and Media Services, you can upload audio and video files to the cloud, then transform them into a group of files that will be ready to stream.
Below, we’ll go through the steps of setting up a stream using Media Services on Azure with Ruby.
Creating Media Services
To start, we need to have an account in Azure. Once that’s done, sign into Azure and search for Media Services in the Services list, then click on the Create button.
Fill in the fields according to what is asked. I usually choose to start my resources with the initials of the kind of service (e.g. Resource Group = rgstreming). We can create a new resource or use an existing one.
Configure Endpoint
We also must configure the endpoints. In the same resource where Media Services exists, we need to create an endpoint and associate it with our Media Services. For the Origin option in the sidebar, choose Storage as Origin Type. And for Origin hostname, select the storage account that you want to use to store your files.
Configuration Ruby to Use Azure Media Services
We have to install Ruby and create a folder (e.g., ruby_streaming_azure) with a Ruby file where we will write the Ruby commands to connect on Azure and send our file to stream. Or we can use the IRB to write the commands and execute immediately. On the terminal, execute the code below to install the Azure Media Services Ruby gem:
$ gem install azure_media_service
Connect Azure Media Services with Ruby
Access Media Services in the sidebar and Account Keys. The account name will be used as the ID and the primary key will be used as secret params.
Now, on the IRB, import the gem to use its commands:
require ‘azure_media_service’
Then, paste the code with your information obtained on the Azure portal:
AzureMediaService.configure do |config|
config.id = ‘YOUR_ACCOUNT_NAME’
config.secret = ‘YOUR_PRIMARY_KEY’
end
Upload File to Azure Media Services
Start the Azure Media Service:
ams = AzureMediaService.service
Media Services supports multiple audio and video formats such as: asf, .avi, .m2tf, .m2v, .mp4, .mpeg, .mpg, .mts, .ts, .wmv, .3gp, .3g2, .3gp2, .Mod, .dv, .vob, .ismv, .m4a.
Copy the path of a supported file to upload the file to Media Services:
asset = ams.upload_media(‘PATH_OF_YOUR_MP3_FILE’)
The file will be stored in Assets.
Encoding the File
After the upload, the file will be ready to be downloaded. But for streaming, we need to encode the file. For this, we need to inform the type of encoding to the new file.
job = asset.encode_job(‘H264 Smooth Streaming 720p’)
A folder inside Assets will be created with the file that was uploaded, along with the files generated after encoding.
Publishing the File
After the job of encoding is finished, we need to perform the last step to publish our file to be available for the stream. To do this, we only need to set the expiration time to our file, and publish with this code:
asset.publish(expire_minutes: 43200)
Now, in Assets, we can see the URLs streaming to each file. We can play them directly in the Azure Portal to test, or copy the URL in Azure Media Player.
Conclusion
Azure Media Services offers a way to stream files that supports Ruby and other languages. Like other services, this one is available via a Ruby gem to facilitate communication and utilization. Azure Media Services is not the most well-known service on the Azure cloud, but it is a good option to consider when you need this kind of streaming solution for a project.