Class: Zencodable::Encoder::Job

Inherits:
Zencoder::Job
  • Object
show all
Defined in:
lib/zencodable.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_id) ⇒ Job

Returns a new instance of Job.



179
180
181
182
# File 'lib/zencodable.rb', line 179

def initialize(job_id)
  @id = job_id
  @job_detail = {}
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



121
122
123
# File 'lib/zencodable.rb', line 121

def id
  @id
end

#mock_all_requestsObject

Returns the value of attribute mock_all_requests.



123
124
125
# File 'lib/zencodable.rb', line 123

def mock_all_requests
  @mock_all_requests
end

Class Method Details

.build_encoder_output_options(origin, settings) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/zencodable.rb', line 136

def build_encoder_output_options(origin, settings)

  formats = settings[:formats] || [:mp4]

  bucket_name = settings[:bucket] || s3_bucket_name(settings[:s3_config])

  s3_base_url = s3_url(origin, bucket_name, settings[:path])

  defaults = { :public => true, :mock => self.mock_request? }

  defaults[:size] = settings[:output_dimensions] if settings[:output_dimensions]

  defaults = defaults.merge(settings[:options]) if settings[:options]

  output_settings = formats.collect{ |f| defaults.merge( :format => f.to_s, :label => f.to_s, :base_url => s3_base_url ) }

  if settings[:thumbnails]
    output_settings[0][:thumbnails] = {:base_url => s3_base_url}.merge(settings[:thumbnails])
  end
  output_settings

end

.create(origin, encoder_definitions) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/zencodable.rb', line 127

def create(origin, encoder_definitions)
  response = super(:input => origin,
                   :outputs => build_encoder_output_options(origin, encoder_definitions))
  if response.code == 201
    job_id = response.body['id']
    self.new(job_id)
  end
end

.mock_request?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/zencodable.rb', line 173

def mock_request?
  (Rails.env == 'test' || self.mock_all_requests)
end

.s3_bucket_name(s3_config_file) ⇒ Object



166
167
168
169
170
171
# File 'lib/zencodable.rb', line 166

def s3_bucket_name s3_config_file
  s3_config_file ||= "#{Rails.root}/config/s3.yml"
  @s3_config ||= YAML.load_file(s3_config_file)[Rails.env].symbolize_keys
  puts @s3_config.inspect
  @s3_config[:bucket_name]
end

.s3_url(origin_url, bucket, path) ⇒ Object



159
160
161
162
163
164
# File 'lib/zencodable.rb', line 159

def s3_url origin_url, bucket, path
  basename = origin_url.match( %r|([^/][^/\?]+)[^/]*\.[^.]+\z| )[1] # matches filename without extension
  basename = basename.downcase.squish.gsub(/\s+/, '-').gsub(/[^\w\d_.-]/, '') # cheap/ugly to_url
  path = path.gsub(%r|:basename\b|, basename)
  "s3://#{bucket}.s3.amazonaws.com/#{path}/"
end

Instance Method Details

#detailsObject



185
186
187
188
189
190
191
192
193
# File 'lib/zencodable.rb', line 185

def details
  if @job_detail.empty? and @id
    response = self.class.details @id
    if response.code == 200
      @job_detail = response.body['job']
    end
  end
  @job_detail
end

#filesObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/zencodable.rb', line 203

def files
  if outfiles = self.details['output_media_files']
    outfiles.collect { |f| { :url =>              f['url'],
                             :format =>           f['label'],
                             :zencoder_file_id => f['id'],
                             :created_at =>       f['finished_at'],
                             :duration_sec =>     f['duration_in_ms'],
                             :width =>            f['width'],
                             :height =>           f['height'],
                             :file_size =>        f['file_size_bytes'],
                             :error_message =>    f['error_message'],
                             :state =>            f['state'] }
                     }
  end
end

#finished_atObject



199
200
201
# File 'lib/zencodable.rb', line 199

def finished_at
  self.details['finished_at']
end

#statusObject



195
196
197
# File 'lib/zencodable.rb', line 195

def status
  self.details['state']
end

#thumbnailsObject

ZC gives thumbnails for each output file format, but gives them the same name and overwrites them at the same S3 location. So if we have f formats, and ask for x thumbnails, we get x*f files described in the details API, but there are actually only x on the S3 server. So, the inject() here is done to pare that down to unique URLs, and give us the cols/vals that paperclip in VideoThumbnail is going to want



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/zencodable.rb', line 225

def thumbnails
  if thumbs = self.details['thumbnails']

    thumbs.inject([]) do |res,th|
      unless res.map{ |r| r[:thumbnail_file_name] }.include?(th['url'])
        res << { :thumbnail_file_name =>   th['url'],
                 :thumbnail_content_type =>th['format'],
                 :thumbnail_file_size =>   th['file_size_bytes'],
                 :thumbnail_updated_at =>  th['created_at']
               }
      end
      res
    end

  end
end