Class: Zencodable::Encoder::Job
- Inherits:
-
Zencoder::Job
- Object
- Zencoder::Job
- Zencodable::Encoder::Job
- Defined in:
- lib/zencodable.rb
Class Attribute Summary collapse
-
.mock_all_requests ⇒ Object
Returns the value of attribute mock_all_requests.
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
Class Method Summary collapse
- .build_encoder_output_options(origin_file) ⇒ Object
- .create(origin_file) ⇒ Object
- .file_basename(origin_url) ⇒ Object
- .mock_request? ⇒ Boolean
- .s3_bucket_name(s3_config_file) ⇒ Object
- .s3_url(origin_file, bucket) ⇒ Object
Instance Method Summary collapse
- #details ⇒ Object
- #files ⇒ Object
- #finished_at ⇒ Object
-
#initialize(job_id) ⇒ Job
constructor
A new instance of Job.
- #status ⇒ Object
-
#thumbnails ⇒ Object
ZC gives thumbnails for each output file format, but gives them the same name and overwrites them at the same S3 location.
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_requests ⇒ Object
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
#id ⇒ Object
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 origin_file settings = origin_file.class. 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 => (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
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.[: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
#details ⇒ Object
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 |
#files ⇒ Object
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_at ⇒ Object
208 209 210 |
# File 'lib/zencodable.rb', line 208 def finished_at self.details['finished_at'] end |
#status ⇒ Object
204 205 206 |
# File 'lib/zencodable.rb', line 204 def status self.details['state'] end |
#thumbnails ⇒ Object
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 |