Decrypt HTTP Live Streaming TS files
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]) |
This comment has been minimized.
This comment has been minimized.
ISTM the load_key() function already returns a nice hexadecimal string, so no need to sprintf convert it again. |
This comment has been minimized.
This comment has been minimized.
any way to fix this? im getting the same error @5ulo got |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
This ruby script isn't working for me.. Am I doing something wrong?
$ ruby decrypt.rb test.m3u8
'decrypt.rb:35:in
initialize': no implicit conversion of nil into String (TypeError) from decrypt.rb:35:in
open'from decrypt.rb:35:in
load_key' from decrypt.rb:29:in
decrypt'from decrypt.rb:19:in
block (2 levels) in read_m3u8' from decrypt.rb:5:in
each_line'from decrypt.rb:5:in
block in read_m3u8' from decrypt.rb:2:in
open'from decrypt.rb:2:in
read_m3u8' from decrypt.rb:40:in