How to Utilize Amazon Elastic Transcoder with Shrine

Amazon Elastic Transcoder is a powerful tool for converting media files stored on Amazon S3 into different formats. The Shrine gem, on the other hand, provides a flexible and feature-rich solution for managing file uploads in Ruby applications. This blog post will delve into how to use these two together to manage and transcode media files in your Ruby application.

How to Utilize Amazon Elastic Transcoder with Shrine

Amazon Elastic Transcoder is a powerful tool for converting media files stored on Amazon S3 into different formats. The Shrine gem, on the other hand, provides a flexible and feature-rich solution for managing file uploads in Ruby applications. This blog post will delve into how to use these two together to manage and transcode media files in your Ruby application.

What is Amazon Elastic Transcoder?

Amazon Elastic Transcoder is a media transcoding service in the cloud. It is designed to convert media files from their source format into versions that will play back on devices like smartphones, tablets, and PCs. Elastic Transcoder lets you create a pipeline specifying the input bucket (source) and output bucket (destination), and your transcoding settings.

What is Shrine?

Shrine is a toolkit for handling file attachments in Ruby applications. It offers a high-level API with the ability to easily customize and extend the gem’s functionality. Shrine supports direct uploads, background processing, and on-the-fly transformations, among other features.

Setting up Amazon Elastic Transcoder

To start using the Amazon Elastic Transcoder, you need to set it up in your AWS console. Create a new pipeline and specify your input and output buckets. Make a note of your pipeline’s ID — you’ll need this in your Ruby application.

Installing Shrine

To add Shrine to your Ruby application, add gem 'shrine', '~> 3.0' to your Gemfile and run bundle install.

Next, configure Shrine in an initializer:

require "shrine"
require "shrine/storage/s3"

s3_options = { 
  access_key_id:     "<your-access-key-id>", 
  secret_access_key: "<your-secret-access-key>", 
  region:            "<your-region>", 
  bucket:            "<your-bucket>", 
}

Shrine.storages = { 
  cache: Shrine::Storage::S3.new(prefix: "cache", **s3_options), 
  store: Shrine::Storage::S3.new(prefix: "store", **s3_options), 
}

Transcoding Media Files

To transcode media files, you’ll use the aws-sdk-elastictranscoder gem. Add it to your Gemfile and run bundle install.

Next, create an Elastic Transcoder client:

transcoder = Aws::ElasticTranscoder::Client.new(
  region: "<your-region>",
  access_key_id: "<your-access-key-id>",
  secret_access_key: "<your-secret-access-key>"
)

To transcode a file, you need to create a job:

job = transcoder.create_job(
  pipeline_id: "<your-pipeline-id>",
  input: { key: "<your-file-key>" },
  outputs: [
    { key: "<your-output-key>", preset_id: "<your-preset-id>" }
  ]
)

The preset_id is a predefined set of transcoding settings. Amazon Elastic Transcoder provides a range of presets for common use cases.

Integrating with Shrine

To integrate the Elastic Transcoder with Shrine, you can create a Shrine plugin:

module Shrine
  module Plugins
    module ElasticTranscoder
      module InstanceMethods
        def transcode(io, context)
          job = transcoder.create_job(
            pipeline_id: "<your-pipeline-id>",
            input: { key: io.key },
            outputs: [
              { key: "<your-output-key>", preset_id: "<your-preset-id>" }
            ]
          )
          
          # return a new Shrine::UploadedFile object
          self.class.uploaded_file(storage: :store, id: job.outputs.first.key)
        end
      end
    end
  end
end

Shrine.plugin :elastic_transcoder

With this setup, you can now transcode files during upload:

class MyUploader < Shrine
  Attacher.promote_block do
    transcode(io, context)
  end
end

That’s it! You’ve now integrated Amazon Elastic Transcoder with Shrine in your Ruby application.

In conclusion, Amazon Elastic Transcoder and Shrine are powerful tools for handling media files. With the guidance provided in this blog post, you should be able to configure them to work together seamlessly in your Ruby application, providing a solid foundation for managing and transcoding media files.


About Saturn Cloud

Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.