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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
# File 'lib/flatiron-video-uploader/runner.rb', line 42
def self.upload_video(file,batch)
begin
`ffmpeg > /dev/null 2>&1`
rescue
puts "Looks like you don't have ffmpeg. Install with brew install ffmpeg"
exit
end
begin
http = Net::HTTP.new("accounts.google.com",443)
http.use_ssl = true
req = Net::HTTP::Head.new('/')
http.start {http.request(req)}
rescue OpenSSL::SSL::SSLError => e
puts "Looks like your version of ruby has messed up SSL Certs."
puts "Please install openssl with brew install openssl"
puts "Then re-install ruby without binaries with rvm reinstall #{RUBY_VERSION} --disable-binary"
exit
end
FFMPEG.logger=Logger.new(STDOUT)
FFMPEG.logger.level = Logger::ERROR
begin
token_response = YAML.load_file(File.expand_path('~/.flatiron-uploader'))
rescue
puts "We are going to need a few ID/Secret stuff."
puts "\n\nGoogle"
google_client_id = THOR_SHELL.ask("What is your Google Client ID?")
google_client_secret = THOR_SHELL.ask("What is your Google Client Secret?")
puts "Finished with Google.\n\n"
puts "#"*30
puts "\n\nAmazon S3"
amazon_s3_key = THOR_SHELL.ask("What is your Amazon S3 Key?")
amazon_s3_secret = THOR_SHELL.ask("What is your Amazon S3 Secret?")
puts "#"*30
response = HTTParty.post("https://accounts.google.com/o/oauth2/device/code",:query => {:scope => "https://www.googleapis.com/auth/youtube.upload https://www.googleapis.com/auth/youtube", :client_id => "404828420863-l0nvu5ihjg7kji18d4pghrnetre28d5e.apps.googleusercontent.com"})
puts "OK cool, go to #{response["verification_url"]} and type in the following code"
`open #{response["verification_url"]}`
puts "#"*30
puts response["user_code"]
puts "#"*30
puts "polling"
token_response = HTTParty.post("https://www.googleapis.com/oauth2/v3/token", :query => {:client_id => google_client_id, :client_secret => google_client_secret, :grant_type => GRANT_TYPE, :code => response["device_code"]})
while(token_response.code==400)
sleep(response["interval"])
token_response = HTTParty.post("https://www.googleapis.com/oauth2/v3/token", :query => {:client_id => google_client_id, :client_secret => google_client_secret, :grant_type => GRANT_TYPE, :code => response["device_code"]})
end
puts "Found!"
token_response["google_client_id"] = google_client_id
token_response["google_client_secret"] = google_client_secret
token_response["amazon_s3_key"] = amazon_s3_key
token_response["amazon_s3_secret"] = amazon_s3_secret
File.open(File.expand_path('~/.flatiron-uploader'), 'w') do |h|
h.write token_response.to_yaml
end
end
if !File.exist?(file) || !File.file?(file)
puts "Not a file!"
exit
end
progress_mutex = Mutex.new
print_progress = false
movie_transcode_thread = Thread.new do
FFMPEG::Transcoder.timeout=90
movie = FFMPEG::Movie.new(file)
movie.transcode("/tmp/transcoded.mp4","-vf 'scale=-2:720:flags=lanczos' -vcodec libx264 -profile:v main -level 3.1 -preset veryfast -crf 31 -x264-params ref=4 -acodec copy -movflags +faststart") do |prog|
progress_mutex.synchronize do
if print_progress
print "\r"
print sprintf("%.2f",prog*100)
$stdout.flush
end
end
end
TerminalNotifier.notify("Finished Transcoding for iPad")
end
auth = Signet::OAuth2::Client.new(
:authorization_uri => "https://accounts.google.com/o/oauth2/auth",
:token_credential_uri => "https://accounts.google.com/o/oauth2/token",
:client_id => token_response['google_client_id'],
:client_secret => token_response['google_client_secret']
)
auth.refresh_token = token_response["refresh_token"]
auth.refresh!
client = Google::APIClient.new(
:application_name => "flatiron_uploder",
:application_version => "0.1")
client.authorization = auth
video_title= ""
topics = []
answer = "N"
while (answer.upcase != "Y") do
temp = THOR_SHELL.ask("What is the name of this video(#{video_title})?")
video_title = temp unless temp.empty?
if (batch.empty?)
puts "#"*30
puts "\n\nUploading Topic Video!\n\n"
puts "#"*30
end
temp = THOR_SHELL.ask("Comma separated list of topics (#{topics})?")
topics = temp.split(",") unless temp.empty?
puts "#"*30
puts "Summary"
puts "Title: #{video_title}"
puts "Batch: #{batch}" unless batch.empty?
puts "Topics: #{topics}"
answer=THOR_SHELL.ask("Is that correct (Y/N)?")
end
youtube = client.discovered_api("youtube","v3")
s3 = Aws::S3::Client.new(
credentials: Aws::Credentials.new(token_response['amazon_s3_key'], token_response['amazon_s3_secret']),
region: 'us-east-1'
)
topics << batch unless batch.empty?
body = {
:snippet => {
:title => video_title,
:tags => topics
},
:status => {
:privacyStatus => "unlisted"
}
}
puts "\n\nOK, about to upload the video. This will take a while"
begin
video_to_upload = Google::APIClient::UploadIO.new(file, 'video/*')
video_to_upload.chunk_size =10000000
progressbar = ProgressBar.create(:title => "Youtube Upload", :total =>video_to_upload.length)
videos_insert_response = client.execute(
:api_method => youtube.videos.insert,
:body_object => body,
:media => video_to_upload,
:retries => 0,
:parameters => {
:uploadType => 'resumable',
:part => body.keys.join(','),
}
)
while !videos_insert_response.resumable_upload.complete? do
progressbar.progress += video_to_upload.chunk_size
videos_insert_response = client.execute(videos_insert_response.resumable_upload)
end
progressbar.finish
puts "Uploaded wooooooooo. The id is #{videos_insert_response.data.id}"
puts "\n\n"
puts "#"*30
puts "Place this in todays schedule on github\n\n\n"
command = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/#{videos_insert_response.data.id}?rel=0&modestbranding=1\" frameborder=\"0\" allowfullscreen></iframe><p><a href=\"https://www.youtube.com/watch?v=#{videos_insert_response.data.id}\">#{video_title}</a></p>"
puts command
puts "\n\n"
puts "#"*30
rescue Exception => e
exit
end
TerminalNotifier.notify("Finished Uploading Video to Youtube. Click to copy embed ", :title => "Video Uploader", :execute => `echo '#{command}' | pbcopy`)
puts "Waiting for the video to finish encoding to iPad format\n\n"
raw_upload_thread = nil
if batch.empty?
raw_upload_thread = Thread.new do
raw_obj = s3.bucket('flatiron-school-learn-videos').object("topic-videos-full/#{videos_insert_response.data.id}.mp4")
raw_obj.upload_file(file, acl:'public-read')
end
end
print_progress = true
movie_transcode_thread.join
puts "\nDone transcoding. Let's upload to S3"
bucket = 'flatiron-school-learn-videos'
key = "lecture-videos/#{videos_insert_response.data.id}.mp4"
lock_file = File.open("/tmp/flatiron-uploader.lock","a+")
lock_file.sync=true
exit unless lock_file.flock( File::LOCK_NB | File::LOCK_EX )
file_name = "/tmp/transcoded.mp4"
File.open('/tmp/transcoded.mp4', 'rb') do |file|
if file.size > PART_SIZE
puts "File size over #{PART_SIZE} bytes, using multipart upload..."
input_opts = {
bucket: bucket,
key: key,
}
input_opts_with_acl = input_opts.merge({acl: "public-read"})
lock_hash = {}
total_parts = file.size.to_f / PART_SIZE
current_part=1
begin
existing_file = lock_file.read
lock_hash = JSON.parse existing_file
if lock_hash["file_name"]==file_name && lock_hash["file_size"]==file.size && lock_hash["key"]==key && lock_hash["total_parts"] == total_parts
binding.pry
current_part = lock_hash["current_part"]
else
input_opts_with_upload = input_opts.merge({
:upload_id => lock_hash["upload_id"],
:key => lock_hash["key"],
})
s3.abort_multipart_upload(input_opts_with_upload)
binding.pry
lock_file.truncate(0)
mpu_create_response = s3.create_multipart_upload(input_opts_with_acl)
lock_hash = {"total_parts"=> total_parts,
"current_part" => current_part,
"file_name" =>file_name,
"file_size" => file.size,
"key" => key,
"upload_id" =>mpu_create_response.upload_id,
}
lock_file.puts lock_hash.to_json
end
rescue StandardError => e
mpu_create_response = s3.create_multipart_upload(input_opts_with_acl)
lock_hash = {"total_parts"=> total_parts,
"current_part" => current_part,
"file_name" =>file_name,
"file_size" => file.size,
"key" => key,
"upload_id" =>mpu_create_response.upload_id,
}
lock_file.puts lock_hash.to_json
end
file.each_part(current_part) do |part|
begin
tries ||=3
part_response = s3.upload_part({
body: part,
bucket: bucket,
key: key,
part_number: current_part,
upload_id: lock_hash["upload_id"],
})
percent_complete = (current_part.to_f / total_parts.to_f) * 100
percent_complete = 100 if percent_complete > 100
percent_complete = sprintf('%.2f', percent_complete.to_f)
puts "percent complete: #{percent_complete}"
current_part = current_part + 1
lock_hash["current_part"]=current_part
lock_file.truncate(0)
lock_file.puts lock_hash.to_json
rescue Exception => e
if e.class == Interrupt
puts "LEaving"
lock_file.truncate(0)
lock_file.puts lock_hash.to_json
File.delete(lock_file.path)
exit
end
puts "trying again"
lock_file.truncate(0)
lock_file.puts lock_hash.to_json
retry unless (tries-=1).zero?
puts "Couldn't get it to work, exiting"
File.delete(lock_file.path)
exit
end
end
mark_as_complete(s3,input_opts,lock_hash)
File.delete(lock_file.path)
else
s3.put_object(bucket: bucket, key: key, body: file, acl: 'public-read')
end
end
raw_upload_thread.join unless raw_upload_thread.nil?
puts "All Done!"
TerminalNotifier.notify("All Done!", :title =>"Video Uploader")
`rm /tmp/transcoded.mp4`
end
|