2021-10-08 16:29:35 +01:00
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# Purpose:
2022-09-20 17:48:02 -06:00
# Demonstrates how to create an HLS job using the AWS SDK for Ruby.
2021-10-08 16:29:35 +01:00
2019-01-30 00:21:42 +00:00
# snippet-start:[elastictranscoder.ruby.create_hls_job.import]
2024-09-25 15:25:54 -04:00
require 'aws-sdk-elastictranscoder'
require 'aws-sdk-s3'
2019-01-29 21:49:23 +00:00
2022-09-21 17:00:48 -06:00
def create_elastictranscoder_job ( transcoder_client , s3_resource , pipeline_name )
bucket_name = " transcoder-bucket- #{ rand ( 10 ** 4 ) } "
s3_resource . create_bucket ( bucket : bucket_name )
resp = transcoder_client . create_pipeline ( {
name : pipeline_name , # required
input_bucket : bucket_name , # required
output_bucket : bucket_name ,
2024-09-25 15:25:54 -04:00
role : 'arn:aws:iam::260778392212:role/Elastic_Transcoder_Default_Role' # required
2022-09-21 17:00:48 -06:00
} )
2024-09-25 15:25:54 -04:00
pipeline_id = resp [ 'pipeline' ] [ 'id' ]
2022-09-21 17:00:48 -06:00
# This is the name of the input key that you would like to transcode.
2024-09-25 15:25:54 -04:00
input_key = '.Jabberwocky.mp3'
2022-09-21 17:00:48 -06:00
# HLS Presets that will be used to create an adaptive bitrate playlist.
2024-09-25 15:25:54 -04:00
hls_64k_audio_preset_id = '1351620000001-200071'
hls_0400k_preset_id = '1351620000001-200050'
hls_0600k_preset_id = '1351620000001-200040'
hls_1000k_preset_id = '1351620000001-200030'
hls_1500k_preset_id = '1351620000001-200020'
hls_2000k_preset_id = '1351620000001-200010'
2022-09-21 17:00:48 -06:00
# Additional variables
2024-09-25 15:25:54 -04:00
segment_duration = '2'
output_key_prefix = 'elastic-transcoder-samples/output/hls/'
2022-09-21 17:00:48 -06:00
input = { key : input_key }
2024-09-25 15:25:54 -04:00
output_key = OpenSSL :: Digest :: SHA256 . new ( input_key . encode ( 'UTF-8' ) ) . to_s
2022-09-21 17:00:48 -06:00
hls_audio = {
2024-09-25 15:25:54 -04:00
key : " hlsAudio/ #{ output_key } " ,
2022-09-21 17:00:48 -06:00
preset_id : hls_64k_audio_preset_id ,
2022-09-23 10:17:20 -06:00
segment_duration : segment_duration
2022-09-21 17:00:48 -06:00
}
hls_400k = {
2024-09-25 15:25:54 -04:00
key : " hls0400k/ #{ output_key } " ,
2022-09-21 17:00:48 -06:00
preset_id : hls_0400k_preset_id ,
2022-09-23 10:17:20 -06:00
segment_duration : segment_duration
2022-09-21 17:00:48 -06:00
}
hls_600k = {
2024-09-25 15:25:54 -04:00
key : " hls0600k/ #{ output_key } " ,
2022-09-21 17:00:48 -06:00
preset_id : hls_0600k_preset_id ,
2022-09-23 10:17:20 -06:00
segment_duration : segment_duration
2022-09-21 17:00:48 -06:00
}
hls_1000k = {
2024-09-25 15:25:54 -04:00
key : " hls1000k/ #{ output_key } " ,
2022-09-21 17:00:48 -06:00
preset_id : hls_1000k_preset_id ,
2022-09-23 10:17:20 -06:00
segment_duration : segment_duration
2022-09-21 17:00:48 -06:00
}
hls_1500k = {
2024-09-25 15:25:54 -04:00
key : " hls1500k/ #{ output_key } " ,
2022-09-21 17:00:48 -06:00
preset_id : hls_1500k_preset_id ,
2022-09-23 10:17:20 -06:00
segment_duration : segment_duration
2022-09-21 17:00:48 -06:00
}
hls_2000k = {
2024-09-25 15:25:54 -04:00
key : " hls2000k/ #{ output_key } " ,
2022-09-21 17:00:48 -06:00
preset_id : hls_2000k_preset_id ,
2022-09-23 10:17:20 -06:00
segment_duration : segment_duration
2022-09-21 17:00:48 -06:00
}
outputs = [ hls_audio , hls_400k , hls_600k , hls_1000k , hls_1500k , hls_2000k ]
playlist = {
2024-09-25 15:25:54 -04:00
name : " hls_ #{ output_key } " ,
format : 'HLSv3' ,
2022-09-21 17:00:48 -06:00
output_keys : outputs . map { | output | output [ :key ] }
}
job = transcoder_client . create_job (
2022-09-23 10:17:20 -06:00
pipeline_id : pipeline_id ,
input : input ,
2024-09-25 15:25:54 -04:00
output_key_prefix : " #{ output_key_prefix } #{ output_key } / " ,
2022-09-23 10:17:20 -06:00
outputs : outputs ,
2022-09-21 17:00:48 -06:00
playlists : [ playlist ]
) [ :job ]
2024-09-25 15:25:54 -04:00
puts " HLS job has been created: #{ JSON . pretty_generate ( job ) } "
2022-09-21 17:00:48 -06:00
end
2019-01-30 00:21:42 +00:00
# snippet-end:[elastictranscoder.ruby.create_hls_job.import]
2022-09-21 17:00:48 -06:00
2024-09-25 15:25:54 -04:00
if __FILE__ == $PROGRAM_NAME
2022-09-21 17:00:48 -06:00
transcoder_client = Aws :: ElasticTranscoder :: Client . new
s3_resource = Aws :: S3 :: Resource . new
pipeline_name = " transcoder-pipeline- #{ rand ( 10 ** 4 ) } "
create_elastictranscoder_job ( transcoder_client , s3_resource , pipeline_name )
2022-09-21 17:03:58 -06:00
end