-
-
Save cdarlint/b1198fcd0f82506f26694379d550907f to your computer and use it in GitHub Desktop.
Decrypt HTTP Live Streaming TS files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment