LoginSignup
0
0

More than 3 years have passed since last update.

Lambda x ElasticTranscoderで動画の複数サイズを生成 するサンプルコード

Last updated at Posted at 2019-08-10

これはなに?

ElasticaTranscoderとLambdaを用いて、S3に動画がアップロードされた際に複数サイズの書き出しを行ってくれるLambdaサンプルコードです。
Lambda Rybyでgemを使用する方法はこちらを参考にして下さい。

前提

ElasticTranscoderでの設定は済ませている。
トリガーの設定も出来る。

Lambdaサンプルコード

require 'aws-sdk-elastictranscoder'

def lambda_handler(event:, context:)
    transcoder_client = Aws::ElasticTranscoder::Client.new(region: ENV['REGION'])
    input_mp4 = event['Records'].first['s3']['object']['key']

    # 大きめ動画変換 
    transcoder_client.create_job(
      pipeline_id: ENV['PIPELINE_ID'],
      input: { key: input_mp4 },
      output: {
        #S3上のvideoをDirからインポートされたファイルをresized_video Dirへエクスポート
        key: input_mp4.gsub('video', 'resized_video').gsub('.mp4','_1080.mp4'),
        preset_id: '1351620000001-000001' # 1080px
      }
    )

    # 中くらい動画変換 
    transcoder_client.create_job(
      pipeline_id: ENV['PIPELINE_ID'],
      input: { key: input_mp4 },
      output: {
        key: input_mp4.gsub('video', 'resized_video').gsub('.mp4','_720.mp4'),
        preset_id: '1351620000001-000010' # 720px
      }
    )

    # ちっちゃい動画変換 
    transcoder_client.create_job(
      pipeline_id: ENV['PIPELINE_ID'],
      input: { key: input_mp4 },
      output: {
        key: input_mp4.gsub('video', 'resized_video').gsub('.mp4','_480.mp4'),
        preset_id: '1351620000001-000020' # 480px
      }
    )

    { statusCode: 200 }
end


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0