Class: VideoConverter::Base

Inherits:
Object show all
Defined in:
lib/video_converter/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Base

Returns a new instance of Base.



7
8
9
10
# File 'lib/video_converter/base.rb', line 7

def initialize params
  self.inputs = Array.wrap(params[:input] || params[:inputs]).map { |input|  Input.new(input) }
  self.outputs = Array.wrap(params[:output] || params[:outputs]).map { |output| Output.new(output.merge(:uid => params[:uid] ? params[:uid].to_s : (Socket.gethostname + object_id.to_s))) }
end

Instance Attribute Details

#inputsObject

Returns the value of attribute inputs.



5
6
7
# File 'lib/video_converter/base.rb', line 5

def inputs
  @inputs
end

#outputsObject

Returns the value of attribute outputs.



5
6
7
# File 'lib/video_converter/base.rb', line 5

def outputs
  @outputs
end

Instance Method Details

#clearObject



55
56
57
58
59
60
# File 'lib/video_converter/base.rb', line 55

def clear
  Command.new("cat #{outputs.first.log} >> #{VideoConverter.log} && rm #{outputs.first.log}").execute
  outputs.map { |output| output.options[:passlogfile] }.uniq.compact.each { |passlogfile| Command.new("rm #{passlogfile}*").execute }
  outputs.select { |output| output.type == 'segmented' }.each { |output| Command.new("rm #{output.ffmpeg_output}").execute }
  true
end

#concat(method = nil) ⇒ Object



66
67
68
# File 'lib/video_converter/base.rb', line 66

def concat method = nil
  Ffmpeg.concat(inputs, outputs.first, method)
end

#convertObject

XXX inject instead of each would be better



17
18
19
20
21
# File 'lib/video_converter/base.rb', line 17

def convert
  success = true
  inputs.each { |input| success &&= Ffmpeg.new(input, outputs).run }
  success
end

#encrypt(options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/video_converter/base.rb', line 41

def encrypt(options = {})
  outputs.each do |output|
    case output.drm
    when 'adobe'
      output.options.merge!(options)
      F4fpackager.run(output) or return false
    when 'hls'
      output.options.merge!(options)
      OpenSSL.run(output) or return false
    end
  end
  true
end

#make_screenshotsObject



23
24
25
26
27
28
29
# File 'lib/video_converter/base.rb', line 23

def make_screenshots
  success = true
  outputs.each do |output|
    success &&= VideoScreenshoter.new(output.thumbnails.merge(:ffmpeg => Ffmpeg.bin, :input => output.ffmpeg_output, :output_dir => File.join(output.work_dir, 'thumbnails'))).run if output.thumbnails
  end
  success
end

#muxObject



70
71
72
# File 'lib/video_converter/base.rb', line 70

def mux
  Ffmpeg.mux(inputs, outputs.first)
end

#runObject



12
13
14
# File 'lib/video_converter/base.rb', line 12

def run
  convert && make_screenshots && segment && encrypt && clear
end

#segmentObject



31
32
33
34
35
36
37
38
39
# File 'lib/video_converter/base.rb', line 31

def segment
  success = true
  outputs.select { |output| output.type == 'playlist' }.each do |playlist|
    paths = playlist.streams.map { |stream| stream[:path] }
    group = outputs.select { |output| paths.include?(output.filename) }.unshift(playlist)
    success &&= (playlist.filename.end_with?('m3u8') ? LiveSegmenter : Mp4frag).send(:run, group)
  end
  success
end

#splitObject



62
63
64
# File 'lib/video_converter/base.rb', line 62

def split
  Ffmpeg.split(inputs.first, outputs.first)
end