def read_m3u8(m3u8)
File.open(m3u8, 'r') do |file|
keyfile = nil
iv = 0
file.each_line do |line|
line.chomp!
if line =~ /^#EXT-X-KEY:METHOD=AES-128,URI="(.*?)"(,IV=0x(.*))?/
keyfile = $1
if $2
iv = $3
iv_gen = :random
else
iv_gen = :sequence
end
elsif not line =~ /^#/
in_file = File.join(File.dirname(m3u8), line)
ext = File.extname(in_file)
out_file = File.join(File.dirname(in_file), File.basename(in_file, ext) + '.clear' + ext)
decrypt(in_file, keyfile, iv, out_file)
if iv_gen == :sequence
iv += 1
end
end
end
end
end
def decrypt(in_file, keyfile, iv, out_file)
key = load_key(keyfile)
iv_hex = sprintf('%032x', iv)
%x{openssl aes-128-cbc -d -K #{key} -iv #{iv_hex} -nosalt -in #{in_file} -out #{out_file}}
end
def load_key(keyfile)
File.open(keyfile, 'rb') do |file|
file.read.unpack('H*')[0]
end
end
read_m3u8(ARGV[0])