globocom / m3u8

Python m3u8 Parser for HTTP Live Streaming (HLS) Transmissions
Other
2.03k stars 471 forks source link

With seg.key but seg.key.iv is None #308

Closed HFrost0 closed 1 year ago

HFrost0 commented 1 year ago

When we need AES to decrypt segment, IV is important. In some m3u8 file, iv is not provided while can be generated by a specific rule which I found in decrypt-m3u8-playlist-encrypted-with-aes-128-without-iv

Can we do this process in m3u8 lib instead of leave the seg.key.iv to None (I found this behavior in version 3.2.0)?

bbayles commented 1 year ago

Here is a way to fall back to the Media Sequence if the Initialization Vector is missing:

import m3u8
playlist_text = '...'  # Your text here
parsed_playlist = m3u8.loads(playlist_text)
seq_no = parsed_playlist.media_sequence or 0
for n, segment in enumerate(parsed_playlist.segments, seq_no):
    iv = segment.key.iv or f'0x{n:032x}'
    # do whatever you need to do with the IV

Reference: RFC 8216, Section 5.2

   An EXT-X-KEY tag with a KEYFORMAT of "identity" that does not have an
   IV attribute indicates that the Media Sequence Number is to be used
   as the IV when decrypting a Media Segment, by putting its big-endian
   binary representation into a 16-octet (128-bit) buffer and padding
   (on the left) with zeros.
leandromoreira commented 1 year ago

as always thank you @bbayles

HFrost0 commented 1 year ago

@bbayles @leandromoreira Thanks for your solution👍, is there any plan to put this inside the m3u8 lib instead of letting user do it by themself?

bbayles commented 1 year ago

I opened https://github.com/globocom/m3u8/pull/310, which would make things a bit simpler.

mauricioabreu commented 1 year ago

Thank you @bbayles