タケユー・ウェブ日報

Ruby on Rails や Flutter といったWeb・モバイルアプリ技術を武器にお客様のビジネス立ち上げを支援する、タケユー・ウェブ株式会社の技術ブログです。

AWS::ElasticTranscoder::Client

クラウドメディア変換サービスAmazon Elastic TranscoderAPIクライアントライブラリ。 AWS SDK for Rubyに含まれる。

ElasticTranscoder

http://aws.amazon.com/jp/elastictranscoder/

  • ファイルの変換元、保存先はS3
  • 変換元と変換先を設定したPipelineを作成
  • Pilelineに、変換対象のファイルや書き出し先ファイル名(S3キー)、変換プリセット(コーデックや解像度など)などを含むJobを作成すればあとは自動でやってくれる - コンテンツの長さ(再生時間)に対して課金。変換エラー時は無料。

参考

Amazon Elastic Transcoder ドキュメント AWS SDK for Ruby Documentation

Getting Started with the AWS Elastic Transcoder API in Rails

サンプルコード

下準備

  1. S3バケットの作成
  2. upload/sample.mp4 に動画ファイルアップロード
  3. ElasticTranscoder パイプライン作成

S3

create pipeline

変換元、保存先、サムネイル保存先のバケットは、パイプラインでそれぞれ設定可能。

コード

https://github.com/uzuki05/trygems/tree/master/try-aws-sdk

Gemfile

source 'https://rubygems.org' gem 'aws-sdk', '~> 1.41.0'

aws.yml

access_key_id: youraccesskeyid 
secret_access_key: yoursecretaccesskey 
s3_endpoint: s3-ap-northeast-1.amazonaws.com 
elastic_transcoder_endpoint: elastictranscoder.ap-northeast-1.amazonaws.com

elastic_transcoder.rb

require 'bundler/setup'
require 'aws-sdk'
require 'yaml'
require 'pp'

config = YAML.load File.read(File.join(File.dirname(__FILE__), 'aws.yml'))
AWS.config(config)

PIPELINE_ID = '0000000000000-xxxxxx'
INPUT_KEY = 'upload/sample.mp4'
OUTPUT_KEY = 'encoded/sample.mp4'
THUMBNAIL_PATTERN = 'encoded/sample-{count}'
PRESET_ID_SYSTEM_WEB = '1351620000001-100070'

transcoder = AWS::ElasticTranscoder::Client.new(api_version: '2012-09-25')

response = transcoder.create_job(
    pipeline_id: PIPELINE_ID,
    input: {
        key: INPUT_KEY,
        frame_rate: 'auto',
        resolution: 'auto',
        aspect_ratio: 'auto',
        interlaced: 'auto',
        container: 'auto'
    },
    output: {
        key: OUTPUT_KEY,
        preset_id: PRESET_ID_SYSTEM_WEB,
        thumbnail_pattern: THUMBNAIL_PATTERN,
        rotate: '0',
    }
)

# pp response #=> {
#     :job => {
#         :id => "0000000000000-xxxxxx",
#         :arn => "arn:aws:elastictranscoder:ap-northeast-1:000000000000:job/0000000000000-xxxxxx",
#         :pipeline_id => "0000000000000-xxxxxx",
#         :input => {
#             :key => "upload/sample.mp4",
#             :frame_rate => "auto",
#             :resolution => "auto",
#             :aspect_ratio => "auto",
#             :interlaced => "auto",
#             :container => "auto"
#         },
#         :output => {
#             :id => "1",
#             :key => "encoded/sample.mp4",
#             :thumbnail_pattern => "encoded/sample-{count}",
#             :rotate => "0",
#             :preset_id => "1351620000001-100070",
#             :status => "Submitted",
#             :watermarks => []
#         },
#         :outputs => [
#             {
#                 :id => "1",
#                 :key => "encoded/sample.mp4",
#                 :thumbnail_pattern => "encoded/sample-{count}",
#                 :rotate => "0",
#                 :preset_id => "1351620000001-100070",
#                 :status => "Progressing",
#                 :watermarks => []
#             }
#         ],
#         :playlists => [],
#         :status => "Submitted"
#     },
#     :request_id => "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
# }

while true
  puts response[:job][:status]
  case response[:job][:status]
    when  'Submitted', 'Progressing'
      sleep 10
      response = transcoder.read_job(id: response[:job][:id])
      next
    when 'Error'
      raise response[:job][:output][:status_detail]
    else
      break
  end
end

pp response
# pp response # => {
#     :job =>{
#         :id => "0000000000000-xxxxxx",
#         :arn => "arn:aws:elastictranscoder:ap-northeast-1:000000000000:job/0000000000000-xxxxxx",
#         :pipeline_id => "0000000000000-xxxxxx",
#         :input => {
#             :key => "upload/sample.mp4",
#             :frame_rate => "auto",
#             :resolution => "auto",
#             :aspect_ratio => "auto",
#             :interlaced => "auto",
#             :container => "auto"
#         },
#         :output => {
#             :id => "1",
#             :key => "encoded/sample.mp4",
#             :thumbnail_pattern => "encoded/sample-{count}",
#             :rotate => "0",
#             :preset_id => "1351620000001-100070",
#             :status => "Complete",
#             :duration => 372,
#             :width => 512,
#             :height => 384,
#             :watermarks => []
#         },
#         :outputs => [
#             {
#                 :id => "1",
#                 :key => "encoded/sample.mp4",
#                 :thumbnail_pattern => "encoded/sample-{count}",
#                 :rotate =>"0",
#                 :preset_id => "1351620000001-100070",
#                 :status => "Complete",
#                 :duration => 372,
#                 :width => 512,
#                 :height => 384,
#                 :watermarks => []
#             }
#         ],
#         :playlists => [],
#         :status => "Complete"
#     },
#     :request_id => "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
# }