Class: VideoConverter::OpenSSL

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.binObject

Returns the value of attribute bin.



4
5
6
# File 'lib/video_converter/openssl.rb', line 4

def bin
  @bin
end

.commandObject

Returns the value of attribute command.



4
5
6
# File 'lib/video_converter/openssl.rb', line 4

def command
  @command
end

.encryption_typeObject

Returns the value of attribute encryption_type.



4
5
6
# File 'lib/video_converter/openssl.rb', line 4

def encryption_type
  @encryption_type
end

Class Method Details

.get_encryption_key(output) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/video_converter/openssl.rb', line 43

def self.get_encryption_key(output)
  if output.encryption_key
    File.join(output.work_dir, "video.key")
  elsif output.encryption_key_url
    File.join(output.work_dir, "url.video.key")
  else
    File.join(output.work_dir, "#{output.filename}.key")
  end
end

.get_encryption_key_path(output) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/video_converter/openssl.rb', line 29

def self.get_encryption_key_path(output)
  if output.encryption_key
    File.open(File.join(output.work_dir,'video.key'), 'wb') { |f| f.write output.encryption_key }
    'video.key'
  elsif output.encryption_key_url
    uri = URI(output.encryption_key_url)
    File.open(File.join(output.work_dir,'url.video.key'), 'wb') { |f| f.puts Net::HTTP.get(uri.host, uri.path) }
    output.encryption_key_url
  else
    File.open(File.join(output.work_dir, "#{output.filename}.key"), 'wb') { |f| f.write SecureRandom.hex(8) }
    "#{output.filename}.key"
  end
end

.run(output) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/video_converter/openssl.rb', line 12

def self.run(output)
  success = true
  playlist = File.join(output.work_dir, output.filename)
  File.write(playlist, File.read(playlist).sub("#EXTM3U\n", "#EXTM3U\n#EXT-X-KEY:METHOD=AES-128,URI=\"#{OpenSSL.get_encryption_key_path(output)}\"\n"))
  encryption_dir = FileUtils.mkdir_p("#{output.chunks_dir}_encrypted").first
  chunks = Dir::glob(File.join(output.chunks_dir, "*.ts")).sort do |c1, c2|
    File.basename(c1).match(/\d+/).to_s.to_i <=> File.basename(c2).match(/\d+/).to_s.to_i
  end
  chunks.each_with_index do |chunk, index|
    initialization_vector = "%032x" % index
    success &&= Command.new(command, prepare_params( :chunk => chunk, :encrypted_chunk => File.join(encryption_dir,File.basename(chunk)),
                                                     :initialization_vector => initialization_vector, :encryption_key => OpenSSL.get_encryption_key(output) )).execute
  end
  remove_old_dir(output) if success
  success
end