-2

Nhập môn AWS - Elastic Transcoder và sử dụng AWS-Ruby SDK với Elastic Transcoder

1. What is Amazon Elastic Transcoder?

Amazon Elastic Transcoder là một dịch vụ của amazon web service cho phép bạn chuyển đổi các tập tin media mà bạn đã lưu trữ trong Amazon Simple Storage Service (Amazon S3) thành các tệp media theo các định dạng yêu cầu của thiết bị phát lại người tiêu dùng. Ví dụ: bạn có thể chuyển đổi các tệp đa phương tiện chất lượng cao sang các định dạng mà người dùng có thể phát trên các thiết bị di động, máy tính bảng, trình duyệt web và TV được kết nối.

Elastic Transcoder gồm 4 thành phần chính:

  • Jobs Jobs làm công việc chuyển mã(transcoder). Mỗi job có thể transcode 1 file thành 30 định dạng. Ví dụ: nếu bạn muốn chuyển đổi một tập tin đa phương tiện thành sáu định dạng khác nhau, bạn có thể tạo các tệp trong tất cả sáu định dạng bằng cách tạo một job đơn lẻ. Khi bạn tạo một job, bạn phải chỉ định tên của tệp bạn muốn chuyển mã, tên bạn muốn đặt cho file sau khi transcode và một số cài đặt khác. Đối với mỗi định dạng mà bạn muốn transcode, bạn cũng chỉ định một template, được gọi là preset (xem bên dưới), chứa cài đặt âm thanh và video mà bạn muốn sử dụng cho tệp hoặc tệp đã transcode.
  • Pipelines Pipeline là hàng đợi để quản lý công việc transcode của bạn. Khi bạn tạo một job, bạn chỉ định pipeline nào bạn muốn thêm job vào. Elastic Transcoder sẽ bắt đầu xử lý các job trong pipeline theo thứ tự mà bạn đã thêm chúng. Nếu bạn định cấu hình một job để transcode thành nhiều định dạng, Elastic Transcoder sẽ tạo ra từng tệp cho mỗi định dạng theo thứ tự bạn chỉ định định dạng trong job. Một cấu hình chung là tạo hai pipeline - một cho các job ưu tiên chuẩn, và một cho các job có mức độ ưu tiên cao. Hầu hết các job đi vào pipeline dẫn ưu tiên chuẩn; bạn chỉ cần sử dụng pipeline có mức độ ưu tiên cao khi bạn cần phải ngay lập tức. Nếu một pipeline đã chứa các job khi bạn tạo một job mới, Elastic Transcoder sẽ xếp hàng job mới nhất và bắt đầu xử lý nó ngay sau khi tài nguyên có sẵn cho pipeline đó. Nếu pipeline đã sử dụng tất cả các tài nguyên của nó, Elastic Transcoder bắt đầu xử lý job tiếp theo trong pipeline khi nó kết thúc một trong những job mà nó đang xử lý. Một pipeline có thể xử lý nhiều việc cùng một lúc, và thời gian cần thiết để hoàn thành job thay đổi đáng kể dựa trên kích thước của tệp bạn đang chuyển đổi và thông số kỹ thuật job. Theo đó, job không nhất thiết phải hoàn thành theo thứ tự bạn tạo ra. Bạn có thể tạm thời ngừng việc xử lý bằng cách tạm dừng pipeline.
  • Presets Presets are templates that contain most of the settings for transcoding media files from one format to another. Elastic Transcoder includes some default presets for common formats, for example, several iPod and iPhone versions. You can also create your own presets for formats that aren't included among the default presets. You specify which preset you want to use when you create a job.
  • Notifications Notifications let you optionally configure Elastic Transcoder and Amazon Simple Notification Service to keep you apprised of the status of a job: when Elastic Transcoder starts processing the job, when Elastic Transcoder finishes the job, and whether Elastic Transcoder encounters warning or error conditions during processing. Notifications eliminate the need for polling to determine when a job has finished. You configure notifications when you create a pipeline. Choosing a Region for Your Elastic Transcoder Resources Pipelines and jobs are closely associated with specific regions. When you create pipelines and jobs, they're created in the current region. When you create a new job, you must specify a pipeline in the current region.

You can specify Amazon S3 buckets in a different region than your Elastic Transcoder resources, but we don't recommend it because you'll incur additional charges for transferring files between AWS regions.

2. Getting Started

1. Step 1: Sign Up for AWS

2. Step 2: Create an Amazon S3 Bucket or Two, and Upload a Media File

  • Create an Amazon S3 bucket for the files that you want to transcode (the input bucket) and another bucket for the transcoded files (the output bucket). You can also use the same bucket for the input bucket and the output bucket.

  • To create Amazon S3 buckets and upload a media file

  • Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/.

  • In the Amazon S3 console, click Create Bucket.

  • In the Create Bucket dialog box, enter a bucket name. If you want to create separate input and output buckets, give the bucket an appropriate name.

  • Select a region for your bucket. By default, Amazon S3 creates buckets in the US Standard region. We recommend that you choose a region close to you to optimize latency, minimize costs, or to address regulatory requirements. This is also the region in which you want Elastic Transcoder to do the transcoding.

  • Click Create.

  • If you want to create separate buckets for the files that you are transcoding and the files that Elastic Transcoder has finished transcoding, repeat Step 2 through Step 5.

  • In the Buckets pane, click the name of your input bucket.

  • Click Actions and then click Upload.

  • On the Upload - Select Files page, click Add Files, and upload a media file that you want to transcode.

  • Click Start Upload.

3. Step 3: Create a Pipeline

A pipeline manages the jobs that transcode your files. Typically, you'll create two or more pipelines, for example, to manage standard-priority jobs and high-priority jobs. In this example, you'll create a single pipeline. For more information about pipelines, see Working with Pipelines. To create a pipeline using the Elastic Transcoder console

  • Open the Elastic Transcoder console at https://console.aws.amazon.com/elastictranscoder/.
  • In the navigation bar of the Elastic Transcoder console, select the region in which you want to create the pipeline.
  • In the left pane of the console, click Pipelines.
  • On the Pipelines page, click Create New Pipeline.
  • Enter the applicable values. For more information about each field, see Settings that You Specify When You Create an Elastic Transcoder Pipeline. Note If you want to monitor the progress of your job, specify the Amazon Simple Notification Service (Amazon SNS) topic that you want to notify when Elastic Transcoder begins processing a job and when Elastic Transcoder has completed the job.
  • Click Create Pipeline.

4. Step 4: (Optional) Create a Preset

A preset is a template that contains the settings that you want Elastic Transcoder to apply during the transcoding process, for example, the codec and the resolution that you want in the transcoded file. When you create a job, you specify which preset you want to use. We provide system presets that create media files that play on many common devices and system presets that target specific devices. For maximum compatibility, choose a preset that creates output that plays on a wide range of devices. For optimum quality and file size, choose a preset that creates output for a specific device or class of devices. For the current list of system presets, see the list of presets on the Elastic Transcoder detail page. If none of the system presets contain the settings that you need, you can create your own presets. For more information, see Creating a Preset in Elastic Transcoder.

5. Step 5: Create a Job

A job does the work of transcoding. You specify the name of the file that you want to transcode (the input file), the name that you want Elastic Transcoder to give the transcoded file, the preset that you want Elastic Transcoder to use, and a few other settings. Elastic Transcoder gets the input file from the Amazon S3 input bucket that you specified in your pipeline, transcodes the file, and saves the transcoded file or files in the Amazon S3 output bucket that you specified in the pipeline. For more information about jobs, see Working with Jobs. To create a job using the Elastic Transcoder console

Step 6: Monitor the Progress of Your Job

When you created a pipeline in Step 3: Create a Pipeline, you had the option to configure notifications, so Elastic Transcoder sends a message to an Amazon Simple Notification Service (Amazon SNS) topic when Elastic Transcoder begins processing a job and finishes processing a job. If you configured notifications and if you subscribed to the applicable Amazon SNS topic, you can monitor the progress of your job.

3. Sử dụng AWS-Ruby SDK với Elastic Transcoder

AWS Elastic Transcoder là một dịch vụ rất tiện dụng nếu bạn tìm kiếm một giải pháp hiệu quả về chi phí, có thể mở rộng để Chuyển mã các tệp âm thanh hoặc video của bạn. AWS Elastic Transcoder lấy đầu vào từ một thùng S3, chuyển mã nó và ghi tệp kết quả vào một xô S3 khác - bạn cũng có thể sử dụng cùng một thùng nếu muốn. Đây là một mã rất nhanh chiết xuất từ một trong một dự án tôi đang currenly làm việc trên. Ở đây tệp âm thanh được tải lên .m4a sẽ được chuyển mã thành HLS (HTTP Live Streaming) - các tệp m4a sẽ được tải lên một lần đầu tiên vào một nhóm S3. Hãy gọi nó là "s3.audio.input". Các tệp tin sẽ được lưu trên gốc của đầu vào s3 s3.audio.input / {UUID} .m4a và âm thanh HLS chuyển mã sẽ được lưu trên s3.audio.output / hlsv4 / {UUID} /index.m3u8

#### transcoder.rb #### 
class Transcoder < Message

  STATUS_SUBMITTED =  "Submitted"
  STATUS_PROGRESSING =  "Progressing"
  STATUS_COMPLETE =  "Complete"
  STATUS_CANCELED =  "Canceled"
  STATUS_ERROR =  "Error"

  def initialize(message)
    @message = message
  end

  def create
    uuid = UUID.new
    transcode_job_uuid = uuid.generate

    transcoder = Aws::ElasticTranscoder::Client.new(
      :access_key_id => "YOUR_AWS_ACCESS_KEY_ID",
      :secret_access_key => "YOUR_AWS_SECRET_ACCESS_KEY",
      :region => "us-east-1")

    #HLSV4

    options = {
      pipeline_id: "1432361831290-XXXXX",
      input: {
        key: "ACB685E9-8C3C-4E5D-A49A-8F3BB5298DDC-201509021233.m4a"
        },
      outputs: [
        {
          key: "hls_64k",
          preset_id: '1351620000001-200071',
          segment_duration: "10"
        }
      ],
      playlists: [
        {
          name: "index",
          format: "HLSv4",
          output_keys: ["hls_64k"]
        }
      ],
      output_key_prefix: "hlsv4/#{@message.id}-#{transcode_job_uuid}/"
    }
    job = transcoder.create_job(options)

    @message.update_attributes(
      transcode_job_uuid: transcode_job_uuid,
      transcode_job_id: job.data[:job][:id],
      transcode_job_status: job.data[:job][:status],
      state: Message::STATE_PUBLISHED)

  end

end

Như bạn đã nhận thấy, tôi đang sử dụng UUID để theo dõi dữ liệu được submiited đến S3, đường ống Trancoder với bản ghi đang hoạt động - (cơ sở dữ liệu MySQL). Ví dụ trên ghi dữ liệu công việc kết quả vào đối tượng ghi lại hoạt động ngay sau khi gửi công việc cho dịch vụ chuyển mã. job.data [: job] [: id] là nhận dạng uniqe (Thông thường trông giống như 1441177406727-xxxxx) được trả về từ AWS mà bạn có thể sử dụng để tiếp tục truy vấn API chuyển mã và nhận thông tin chi tiết về công việc được gửi.

@message.update_attributes(
      transcode_job_uuid: transcode_job_uuid,
      transcode_job_id: job.data[:job][:id],
      transcode_job_status: job.data[:job][:status],
      state: Message::STATE_PUBLISHED)

Bạn cũng có thể cấu hình bộ mã hoá đàn hồi để làm việc với SNS. Dịch vụ chuyển mã AWS sẽ kích hoạt thông báo SNS đến một chủ đề được xác định trước dựa trên các sự kiện này. On Warning Event On Completion Event On Error Event Ví dụ một khi công việc chuyển mã đã kết thúc SNS sẽ thông báo trạng thái của công việc cho một điểm cuối mà bạn đã cấu hình trên chủ đề SNS của bạn.

reference: http://docs.aws.amazon.com/elastictranscoder/latest/developerguide/getting-started.html https://randika.com/aws-ruby-sdk-and-elastic-transcoder-example-c6c34fb5bc32


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí