Skip to content

Commit

Permalink
Make CloudFront streaming work
Browse files Browse the repository at this point in the history
  • Loading branch information
phuongdh committed Sep 16, 2016
1 parent 2f7a9f8 commit 4251477
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ GIT

GIT
remote: https://github.com/phuongdh/active_encode.git
revision: 5f8e8fbe8d22575fb08768fb9dcca46e09e92f75
revision: 8536faf0e0ec2b0b14cdcddaacff946287b34f29
branch: elastic_transcoder
specs:
active_encode (0.0.2)
Expand Down
2 changes: 1 addition & 1 deletion app/models/derivative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def set_streaming_locations!
if !!self.managed
path = URI.parse(absolute_location).path
self.location_url = Avalon::StreamMapper.map(path,'rtmp',self.format)
self.hls_url = Avalon::StreamMapper.map(path,'http',self.format)
self.hls_url = Avalon::StreamMapper.map(self.hls_url || path,'http',self.format)
end
self
end
Expand Down
36 changes: 24 additions & 12 deletions app/models/elastic_transcoder_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def upload_to_s3

file_path = self.input.sub("file://", "")
file_name = File.basename file_path
output_file_name = File.basename(file_name, ".*") + ".mp4"
uuid = SecureRandom.uuid
upload_prefix = ENV['S3_UPLOAD_PREFIX']
input_key = "#{upload_prefix}/#{uuid}/#{file_name}"
Expand All @@ -20,33 +21,44 @@ def upload_to_s3
hls_0400k_preset_id = '1351620000001-200050';
hls_1000k_preset_id = '1351620000001-200030';
hls_2000k_preset_id = '1351620000001-200010';
flash_2200k_preset_id = '1351620000001-100210';
flash_1200k_preset_id = '1473357228814-ye0s5g';
flash_2200k_preset_id = '1351620000001-000010';
flash_5400k_preset_id = '1351620000001-000001';

hls_400k = {
key: 'quality-low/' + file_name,
hls_low = {
key: 'quality-low/hls/' + output_file_name,
preset_id: hls_0400k_preset_id,
segment_duration: segment_duration
}

hls_1000k = {
key: 'quality-medium/' + file_name,
hls_medium = {
key: 'quality-medium/hls/' + output_file_name,
preset_id: hls_1000k_preset_id,
segment_duration: segment_duration
}

hls_2000k = {
key: 'quality-high/' + file_name,
hls_high = {
key: 'quality-high/hls/' + output_file_name,
preset_id: hls_2000k_preset_id,
segment_duration: segment_duration
}

flash_2200k = {
key: 'quality-high/' + file_name,
preset_id: flash_2200k_preset_id,
segment_duration: segment_duration
flash_low = {
key: 'quality-low/rtmp/' + output_file_name,
preset_id: flash_1200k_preset_id
}

flash_medium = {
key: 'quality-medium/rtmp/' + output_file_name,
preset_id: flash_2200k_preset_id
}

flash_high = {
key: 'quality-high/rtmp/' + output_file_name,
preset_id: flash_5400k_preset_id
}

outputs = [ hls_400k, hls_1000k, hls_2000k, flash_2200k ]
outputs = [ hls_low, hls_medium, hls_high, flash_low, flash_medium, flash_high ]

extra_options = { pipeline_id: ENV['AWS_PIPELINE_ID'], outputs: outputs, output_key_prefix: "#{ENV['S3_OUTPUT_PREFIX']}/#{uuid}/" }
self.options.merge! extra_options
Expand Down
3 changes: 3 additions & 0 deletions config/initializers/stream_mapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'avalon/cloudfront_mapper'

Avalon::StreamMapper = Avalon::CloudFrontMapper.new
11 changes: 11 additions & 0 deletions lib/avalon/cloudfront_mapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'avalon/stream_mapper'

module Avalon
class CloudFrontMapper < DefaultStreamMapper
def stream_details_for(path)
p = Pathname.new(path)
Detail.new(base_url_for(path,'rtmp'),base_url_for(path,'http'),p.dirname,p.basename(p.extname),p.extname[1..-1])
end
end
end

2 changes: 0 additions & 2 deletions lib/avalon/stream_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,4 @@ def map(path, protocol, format)
template.result(stream_details_for(path).get_binding)
end
end

StreamMapper = DefaultStreamMapper.new
end

0 comments on commit 4251477

Please sign in to comment.