Skip to content

Instantly share code, notes, and snippets.

@refractalize
Created January 4, 2012 20:11
Show Gist options
  • Save refractalize/1561849 to your computer and use it in GitHub Desktop.
Save refractalize/1561849 to your computer and use it in GitHub Desktop.
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])
@5ulo
Copy link

5ulo commented Jul 12, 2013

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:inopen'
from decrypt.rb:35:in load_key' from decrypt.rb:29:indecrypt'
from decrypt.rb:19:in block (2 levels) in read_m3u8' from decrypt.rb:5:ineach_line'
from decrypt.rb:5:in block in read_m3u8' from decrypt.rb:2:inopen'
from decrypt.rb:2:in read_m3u8' from decrypt.rb:40:in

'

@fche
Copy link

fche commented Dec 20, 2016

ISTM the load_key() function already returns a nice hexadecimal string, so no need to sprintf convert it again.

@eitrnel
Copy link

eitrnel commented Jun 19, 2017

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