Search code examples
ruby-on-railsamazon-web-servicescarrierwavesidekiqamazon-elastic-transcoder

How to use Elastic Transcoder with the AWS-SDK for Ruby


I am completely stuck. I need to be walked through the process for using the AWS-SDK for Ruby to transcode video. I'm not sure where to even begin. I'm uploading files using CarrierWave-Direct to an s3 bucket. The records are uploaded and recalled fine. The uploading is done in the background using Sidekiq. Where do I go from here? How do I kick off the Transcoding job? How do I maintain a record of the files for later streaming in my database? Can I transcode along with my uploading Sidekiq process? I'm ripping my hair out for trying to find a solution for this.


Solution

  • The transcoding actions are defined inside the class AWS::ElasticTranscoder.

    Transcoding process can be initiated once your upload to s3 has been completed. For a simple transcoder to work, you need to have

    1. A pipeline on which the transcoding will be carried out.
    2. Presets which determines the output video properties (You can either create a preset or use the system presets provided by AWS)

    Now initiate AWS::ElasticTranscoder::Client class

    transcoder = AWS::ElasticTranscoder::Client.new(:access_key_id => AwsKeyId,:secret_access_key => AwsAccessKey,:region=>TranscoderRegion)
    

    Create pipeline:

    transcoder.create_pipeline(options = {
            :name => “test_pipeline”,
            :input_bucket => “bucket_name” , 
            :output_bucket => “bucket_name”,
            :role => Elastic-Transcoder-Default-Role,
            :content_config => {
                :bucket => “bucket_name”,
                :storage_class => Standard
            }
            :thumbnail_config => {
                :bucket => “bucket_name”,
                :storage_class => Standard 
            }
    })
    

    This will return a pipelineId that can be used for creating jobs.

    Now you can create a job as follows:

     transcoder_obj.create_job({
        :pipeline_id=>PipelineId,
        :input=> {
           :key=>"video_path",
           :frame_rate=> "auto",
           :resolution => "auto",
           :aspect_ratio => "auto",
           :container => 'auto'
        },
        :outputs=>[{
           :key=>"output_file_location",
           :preset_id=>1351620000001-000010,
           :thumbnail_pattern=>"thumbnails/thumb_{count}"
        }]
    })
    

    This will start the transcoding process. You can check the status of the job by using read_job method. Once the status becomes 'Completed' from 'Progressing', the output files will be there in the specified output bucket.

    Please go through these links:

    http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/ElasticTranscoder/Client.html http://docs.aws.amazon.com/elastictranscoder/latest/developerguide/system-presets.html

    AWS::SNS provides a better way of getting notified about the transcoding job status. You can subscribe to an sns topic for getting the jobs status. The response for a completed job will contain the necessary details about the output files, so that you can store it in database for future streaming.

    For more info, refer this link