How to download aes encrypted m3u8 using ffmpeg or youtube-dl?

I have the m3u8 file and the key.bin, inside the m3u8 file is:

#EXTM3U
#EXT-X-VERSION:4
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-TARGETDURATION:6
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-KEY:METHOD=AES-128,URI="key.bin"
#EXTINF:6.000000,
segment-0.ts
...
#EXTINF:4.416667,
segment-159.ts
#EXT-X-ENDLIST

and inside the key.bin file is

Ú{±€rl”ÜJÌy«‡mó

How do I download this as an mp4 using ffmpeg or youtube-dl?.

2

3 Answers

You can use ffmpeg like so:

ffmpeg -i video.m3u8 -c copy -bsf:a aac_adtstoasc video.mp4

Also if the urls are https you can add https to allowed protocols like so:

ffmpeg -protocol_whitelist file,http,https,tcp,tls,crypto -i video.m3u8 -c copy -bsf:a aac_adtstoasc video.mp4

Original article:Download encrypted HLS content with ffmpeg - davd.io

If you encounter a field such as EXT-X-KEY, the URI value first needs to be resolved, relative to the playlist URI. So if you have this:

then the resultant URI will be:

once thats done, you can get the data from the URI, typically 16 bytes or more. You can then resolve a segment URI:

and download as normal. Next you can decrypt the segment using AES-CBC, using the data from key.bin, as both the AES key, and the CBC IV. After decrypting, you'll need to remove any padding, if it exists. Repeat this process for all the segments.

RFC 8216 - EXT-X-KEY

If you're using youtube-dl -o file.mp4 "m3u8-URL", you might need to might need to pass proper headers for youtube-dl/ffmpeg to be allowed to download the key. use the --user-agent "USERAGENT" and --referer "REFERRER_URL" argument. You need to use same user-agent as your browser-you can find yours easily at and referer is the main URL in the browser from where you sniffed out the m3u8 URL)

Credit goes to the guy from reddit who gave the solution.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like