title: decrypt .ts segments from m3u8 with python
body: <p>Why does my code not decrypt the <code>ts</code> segment?</p>
  
  <pre><code>from Crypto.Cipher import AES
  import requests as req
  import os
  import m3u8
  with req.session() as req:
      m3u = m3u8.load('https://svt1-a.akamaized.net/se/svt1/d132b755-8088-475e-91df-2c8f2de9cf6c/hls-v6/v6.m3u8')
      key_url = m3u.keys[0].absolute_uri
      seg = m3u.segments[0].absolute_uri
      key = ''
      for chunk in req.get(key_url,stream=True):
          key+=chunk
      cipher = AES.new(key, AES.MODE_CBC, os.urandom(16))
      data = ''
      for chunk in req.get(seg, stream=True):
          data+=chunk
      data = cipher.decrypt(data)
      with open('ulk.ts','wb') as fil:
          fil.write(data)
  </code></pre>
  
  <p>The m3u8 url is: </p>
  
  <blockquote>
    <p><a href="https://svt1-a.akamaized.net/se/svt1/d132b755-8088-475e-91df-2c8f2de9cf6c/hls-v6/v6.m3u8" rel="nofollow noreferrer">https://svt1-a.akamaized.net/se/svt1/d132b755-8088-475e-91df-2c8f2de9cf6c/hls-v6/v6.m3u8</a></p>
  </blockquote>
  
  <p>Code from the m3u8:</p>
  
  <pre><code>EXTM3U
  #EXT-X-VERSION:4
  #EXT-X-TARGETDURATION:4
  #EXT-X-MEDIA-SEQUENCE:220662
  #EXTINF:3.20000,
  #EXT-X-KEY:METHOD=AES-128,URI="../key-d132b755-8088-475e-91df-2c8f2de9cf6c.ts"
  v6-220662.ts
  #EXTINF:3.20000,
  v6-220663.ts
  #EXTINF:3.20000,
  v6-220664.ts
  #EXTINF:3.20000,
  v6-220665.ts
  #EXTINF:3.20000,
  v6-220666.ts
  #EXTINF:3.20000,
  v6-220667.ts
  #EXTINF:3.20000,
  v6-220668.ts
  #EXTINF:3.20000,
  v6-220669.ts
  #EXTINF:3.20000,
  v6-220670.ts
  #EXTINF:3.20000,
  v6-220671.ts
  </code></pre>
  
  <p>Keystring:</p>
  
  <blockquote>
    <p>'PB{\xf1\xb9\xf9.\xb7\x1d\xeb\x95\x8f=\xcd\x93\xce'</p>
  </blockquote>