Class: FlatironVideoUploader::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/flatiron-video-uploader/runner.rb

Constant Summary collapse

GRANT_TYPE =
"http://oauth.net/grant_type/device/1.0"
THOR_SHELL =
Thor::Shell::Basic.new

Class Method Summary collapse

Class Method Details

.upload_video(file, batch) ⇒ Object



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
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
# File 'lib/flatiron-video-uploader/runner.rb', line 6

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 
    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 23 -x264-params ref=4 -acodec copy -movflags +faststart") do |prog|
      progress_mutex.synchronize do
        if print_progress 
          print "\r"
          # puts add \n to the end of string, use print instead
          print sprintf("%.2f",prog*100)

          # force the output to appear immediately when using print
          # by default when \n is printed to the standard output, the buffer is flushed.
          $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::Resource.new(
    # credentials: Aws::Credentials.new('AKIAJY4YSCUKNRWWASIA', 'lXp+rsDPaHcoaEay3XcjfAnE1l1W8BRWOrdLfsPH'),
     credentials: Aws::Credentials.new(token_response['amazon_s3_key'], token_response['amazon_s3_secret']),
    region: 'us-east-1'
  )

  topics << batch unless batch.empty?
  # begin
    body = {
      :snippet => {
        :title => video_title,
        :tags => topics
      },
      :status => {
        :privacyStatus => "unlisted"
      }
    }

    puts "\n\nOK, about to upload the video. This will take a while"
    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
    TerminalNotifier.notify("Finished Uploading Video to Youtube. Click to copy embed ", :title => "Video Uploader", :execute => `echo '#{command}' | pbcopy`)
  # rescue Google::APIClient::TransmissionError => e
  #   binding.pry
  #   puts e.result.body
  # end

  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"

  obj = s3.bucket('flatiron-school-learn-videos').object("lecture-videos/#{videos_insert_response.data.id}.mp4")
  obj.upload_file('/tmp/transcoded.mp4', acl:'public-read')
  raw_upload_thread.join unless raw_upload_thread.nil?
  puts "All Done!"
  TerminalNotifier.notify("All Done!", :title =>"Video Uploader")
  `rm /tmp/transcoded.mp4`
end