Class: Alula::VideoProcessor

Inherits:
Processor show all
Defined in:
lib/alula/processors/video.rb

Direct Known Subclasses

Zencoder

Instance Attribute Summary

Attributes inherited from Processor

#attachments, #item, #options, #site

Instance Method Summary collapse

Methods inherited from Processor

#asset_path, available?, #cleanup, #info, #initialize, mimetype, process?

Constructor Details

This class inherits a constructor from Alula::Processor

Instance Method Details

#processObject



3
4
5
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
# File 'lib/alula/processors/video.rb', line 3

def process
  # Select variants we need to generate
  generate = variants.select do |name, format|
    size = format[:size].split("x").collect{|i| i.to_i}
    
    # Check if video requires rotating
    if self.info.rotation == 90 or self.info.rotation == 270
      size.reverse!
    # elsif self.info.height > self.info.width  # We have pre-rotated portait video, match framesize
    #   size.reverse!
    end
    # Write rotated values back
    format[:size] = size.join("x")
    width, height = size
    
    # Generate attachment name hash
    ext = (format[:mobile] ? "-mobile" : "") + (format[:hires] ? "-hires" : "") + ".#{format[:format]}"
    name = File.join "video", item.name.gsub(/\.#{item.extension}$/, "#{ext}")
    
    output = asset_path(name, :video)
    
    format[:output] = output
    
    # Skip video processing if output already exists...
    !File.exists?(output) and !((width > self.info.width and height > self.info.height) and format[:hires])
  end
  
  # Detect if thumbnail is required
  size = self.site.config.attachments.video["thumbnail"].split("x").collect{|x|x.to_i}
  thumbnails = ([{size: size, hires: false}] + [{size: size.collect{|x| x*2}, hires: true}])
    .collect {|tn|
      if self.info.rotation == 90 or self.info.rotation == 270
        tn[:size].reverse!
      end
      tn[:size] = tn[:size].join("x")
      name = File.join "thumbnail", item.name.gsub(/\.#{item.extension}$/, "#{tn[:hires] ? "-hires" : ""}.png")
      tn[:output] = asset_path(name, :thumbnail)
      tn[:label] = "thumbnail#{tn[:hires] ? "-hires" : ""}"
      tn
    }
    .select {|tn| !tn[:hires] or (tn[:hires] and self.site.config.attachments.image.hires)}
    .select{|tn|
      width, height = tn[:size].split("x").collect{|i| i.to_i};
      !File.exists?(tn[:output]) and !((width > self.info.width and height > self.info.height) and tn[:hires])
  }
  
  thumbnails = Hash[thumbnails.collect{|tn| [tn[:label], tn]}]
  
  encode(generate, thumbnails)
end