Class: Zencodable::Encoder::Job

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_id) ⇒ Job

Returns a new instance of Job.



188
189
190
191
# File 'lib/zencodable.rb', line 188

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

Class Attribute Details

.mock_all_requestsObject

Returns the value of attribute mock_all_requests.



125
126
127
# File 'lib/zencodable.rb', line 125

def mock_all_requests
  @mock_all_requests
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



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

def id
  @id
end

Class Method Details

.build_encoder_output_options(origin_file) ⇒ Object



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
# File 'lib/zencodable.rb', line 136

def build_encoder_output_options origin_file

  settings = origin_file.class.encoding_options

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

  s3_base_url = s3_url(origin_file, (settings[:bucket] || s3_bucket_name(settings[:s3_config])))

  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,
                                                         :filename => file_basename(origin_file.origin_url) + ".#{f}",
                                                         :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_file) ⇒ Object



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

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

.file_basename(origin_url) ⇒ Object



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

def file_basename origin_url
  basename = origin_url.match( %r|([^/][^/\?]+)[^/]*\.[^.]+\z| )[1] # matches filename without extension
  basename.downcase.squish.gsub(/\s+/, '-').gsub(/[^\w\d_.-]/, '') # cheap/ugly to_url
end

.mock_request?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/zencodable.rb', line 182

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

.s3_bucket_name(s3_config_file) ⇒ Object



176
177
178
179
180
# File 'lib/zencodable.rb', line 176

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
  @s3_config[:bucket_name]
end

.s3_url(origin_file, bucket) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/zencodable.rb', line 162

def s3_url origin_file, bucket
  path = origin_file.class.encoding_options[:path]
  path.scan(%r|:[a-z]\w+\b|) do |match|
    method = match.gsub(/^:/,'').to_sym
    path = path.gsub(/#{match}/, origin_file.send(method)) if origin_file.respond_to?(method)
  end
  "s3://#{bucket}.s3.amazonaws.com/#{path.gsub(%r#/\z#, '')}/"
end

Instance Method Details

#detailsObject



194
195
196
197
198
199
200
201
202
# File 'lib/zencodable.rb', line 194

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



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/zencodable.rb', line 212

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



208
209
210
# File 'lib/zencodable.rb', line 208

def finished_at
  self.details['finished_at']
end

#statusObject



204
205
206
# File 'lib/zencodable.rb', line 204

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



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/zencodable.rb', line 234

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