-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOoyalaToBIF.rb
executable file
·161 lines (137 loc) · 3.87 KB
/
OoyalaToBIF.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
require 'optparse'
require 'fileutils'
require 'ooyala-v2-api'
require 'curb'
require 'mini_magick'
require 'aws-sdk'
load 'bif.rb'
# Parse ALL the options
options = {}
optparse = OptionParser.new do |opts|
opts.banner = "Usage: ./OoyalaToBIF.rb -k KEY -s SECRET embed_code1 [embed_code2 embed_code3 ...]"
opts.on("-k", "--key STRING", "Ooyala API Key") do |k|
options[:key] = k
end
opts.on("-s", "--secret STRING", "Ooyala Secret") do |s|
options[:secret] = s
end
opts.on("-w", "--aws STRING:STRING", "AWS AccessKeyID:SecretAccessKey") do |w|
if w =~ /(.*)\:(.*)/
options[:aws] = true
creds = w.split(":")
options[:aws_key] = creds[0]
options[:aws_pass] = creds[1]
else
puts "ABORT: AWS Credentials should be entered as KEY:PASS"
end
end
opts.on_tail('-h', '--help', 'Display this screen') do
puts opts
exit
end
end
begin
optparse.parse!(ARGV)
if ARGV[0].nil?
puts "ABORT: Missing embed_codes\n\n"
puts optparse
exit
end
puts options
mandatory = [:key,:secret]
missing = mandatory.select{ |param| options[param].nil? }
if not missing.empty?
puts "[ABORT] Missing required variables: #{missing.join(', ')}\n\n"
puts optparse
exit
end
rescue OptionParser::InvalidOption, OptionParser::MissingArgument
puts $!.to_s
puts optparse
exit
end
dir = Dir.mktmpdir
ooyala = Ooyala::API.new(options[:key],options[:secret])
# FOR EACH EMBED_CODE
ARGV.each do |asset|
puts "\n[#{asset}] Ooyala: Contacting..."
# Contact Ooyala
call = ooyala.get("/v2/assets/#{asset}/generated_preview_images")
call = call.sort_by{|hash| hash['time']}
puts "[#{asset}] Ooyala: Responded"
urls = {}
i = 0
call.each do |gpi|
filename = File.join(dir,asset,"raw","%04d.jpg") % i
urls[filename] = gpi['url']
i = i + 1
end
FileUtils.mkdir_p(File.join(dir,asset,"raw"))
j = 0
print "[#{asset}] Raw Files: #{j}/#{i}"
# Download ALL the images
urls.each do |filename,url|
f = open(filename,'wb')
begin
curl = Curl::Easy.new(url)
curl.perform
f.write(curl.body_str)
rescue
sleep(3)
retry
ensure
f.close()
j = j + 1
print "\r[#{asset}] Raw Files: #{j}/#{i}"
end
end
FileUtils.mkdir_p(File.join(dir,asset,"hd"))
FileUtils.mkdir_p(File.join(dir,asset,"sd"))
j = 0
# Resize ALL the images, twice for good measure (or, just to appease HD and SD demands)
print "\n[#{asset}] Resizing images: #{j}/#{i}"
image_files = Dir[File.join(dir,asset,"raw","*.jpg")]
image_files.each do |img|
# Open Image
image = MiniMagick::Image.open(img)
# HD
image.resize "320"
image.format "jpg"
image.write File.join(dir,asset,"hd",File.basename(img))
# SD
image.resize "240"
image.format "jpg"
image.write File.join(dir,asset,"sd",File.basename(img))
j = j + 1
print "\r[#{asset}] Resizing images: #{j}/#{i}"
end
puts "\n[#{asset}] BIFs: Building"
# Assemble BOTH BIFs
# HD BIF
bif = BIF::Writer.new(dir: File.join(dir,asset,"hd"))
bif.write(File.join(dir,"#{asset}-HD.bif"))
# SD BIF
bif = BIF::Writer.new(dir: File.join(dir,asset,"sd"))
bif.write(File.join(dir,"#{asset}-SD.bif"))
print "\r[#{asset}] BIFs: Complete!\n"
end
# Upload to S3
if options[:aws] === true
aws = Aws::S3::Client.new(region: 'us-east-1', access_key_id: options[:aws_key], secret_access_key: options[:aws_pass])
bifs = Dir[File.join(dir,"*.bif")]
j = 0
print "\nUploading to S3: #{j}/#{bifs.length}"
bifs.each do |b|
obj = Aws::S3::Object.new('usasfbifs',File.basename(b),{:client => aws})
obj.upload_file(b,{:acl => "public-read"})
j = j + 1
print "\rUploading to S3: #{j}/#{bifs.length}"
end
end
puts "\nCleaning Up..."
# Clean up after yourself, like your mother taught you
FileUtils.remove_entry dir
puts "Finished!\n\n"