Skip to content

Instantly share code, notes, and snippets.

@bgkittrell
Created June 11, 2013 13:57
Show Gist options
  • Save bgkittrell/5757061 to your computer and use it in GitHub Desktop.
Save bgkittrell/5757061 to your computer and use it in GitHub Desktop.
Uploads a directory of files to s3 and creates elastic transcoder jobs. Requires the aws-sdk gem. Usage ruby aws-transcode.rb /path/to/videos
require 'aws'
AWS.config(:access_key_id => 'XXXXX', :secret_access_key => 'XXXXX')
pipeline_id = 'XXXXXXX'
preset_id = 'XXXXXX'
s3 = AWS::S3.new
bin = s3.buckets['XXXXXX-in']
bout = s3.buckets['XXXXXX-out']
transcoder = AWS::ElasticTranscoder::Client.new
jobs = []
threads = []
workstart = Time.now
mutex = Mutex.new
for file in Dir.entries(ARGV[0])
next if file =~ /^\./
threads << Thread.new(file) do |key|
puts "Transcoding: #{key}"
start = Time.now
mutex.synchronize do
obj = bin.objects[key]
obj.write File.open(File.join(ARGV[0], key), 'rb') unless obj.exists?
end
done = Time.now
puts "[#{key}] Uploaded In #{done.to_i - start.to_i}s"
parts = key.split(/\./)
parts.pop
parts << 'm4v'
output = parts.join '.'
job = transcoder.create_job(
pipeline_id: pipeline_id,
input: {
key: key,
frame_rate: 'auto',
resolution: 'auto',
aspect_ratio: 'auto',
interlaced: 'auto',
container: 'auto'
},
output: {
key: output,
preset_id: preset_id,
thumbnail_pattern: "",
rotate: '0'
}
)[:job]
puts "[#{key}] Job Started: #{job[:id]}"
tries = 0
while tries < 1000
response = transcoder.read_job(:id => job[:id])[:job]
if response[:status] == "Error"
puts "[#{key}] Transcoding Error #{response[:status_detail]}"
break
elsif response[:status] == "Complete"
puts "[#{key}] Transcoding Completed in #{Time.now.to_i - done.to_i}s"
break
end
tries += 1
sleep 5
end
end
end
for thread in threads
thread.join
end
puts "Finished in #{Time.now.to_i - workstart.to_i}s"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment