- 
      
- 
        Save doio/3363a7769a79702d45d679df58e092f7 to your computer and use it in GitHub Desktop. 
Revisions
- 
        refractalize created this gist Jan 4, 2012 .There are no files selected for viewingThis 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,40 @@ 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])