As we all know, we can use mp4decrypt or ffmpeg to decrypt CENC-encrypted mp4 files. But what if the file we have is a series of encrypted .ts videos? Recently, I discovered that we can use ffmpeg with a manually written .m3u8 file to decrypt .ts videos. Here's how:
Step 1
Understand which encryption algorithm is used for your video and what its IV is. On the target website, you will capture a request for an m3u8 file. What you need to do is observe the #EXT-X-KEY field in it, which clearly states the algorithm and IV value used. For example:
This indicates that your video is encrypted using the SAMPLE-AES-CTR algorithm and the IV value is 0x62bdf8c903e29da1e690cde43ca18ac6.Code:#EXT-X-KEY:METHOD=SAMPLE-AES-CTR,URI="data:text/plain;base64,AAAApXBzc2gAAAAA7e+LqXnWSs6jXeDBJbjA9MgdkZWZhdWx0",IV=0x62bdf8c903e29da1e690cde43ca18ac6,KEYFORMAT="urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed",KEYFORMATVERSIONS="1"
Step 2
Write the obtained key into a file. Here, I call it key.key. You can use hexed.it to create a 16-byte file and fill it with the obtained key in hexadecimal literal form.
Let's assume that I've got a key: 6762b37e175348bda9582b42ca25f99a. I will create a 16-byte file on hexed.it:
[Attachment 71273 - Click to enlarge]
Then, I will paste this key into the file, select "Overwrite the bytes", and choose "Hexadecimal Values":
[Attachment 71274 - Click to enlarge]
Finally, save the file as key.key:
[Attachment 71275 - Click to enlarge]
Step 3
Manually write an M3U8 file like this and put it in the same folder as all the ts files and key.key file:
The key points of this file are:Code:#EXTM3U #EXT-X-VERSION:5 #EXT-X-TARGETDURATION:6 #EXT-X-KEY:METHOD=SAMPLE-AES-CTR,URI="key.key",IV=0x62bdf8c903e29da1e690cde43ca18ac6 #EXTINF:1, 1.ts #EXTINF:1, 2.ts #EXT-X-ENDLIST
1. Keep the beginning #EXTM3U, #EXT-X-VERSION:5, #EXT-X-TARGETDURATION:6, and ending #EXT-X-ENDLIST unchanged;
2. For the #EXT-X-KEY field, you can copy the #EXT-X-KEY content from the video website, delete all fields except METHOD, URI, and IV, and set the URI value to "key.key";
3. For each .ts file, you need to write a line "#EXTINF:1," and then write the name of the ts file after this line. The value of 1 indicates the length of the ts file in seconds, but you can put any random value in it since ffmpeg will automatically obtain the actual length.
Step 4
Use ffmpeg to read the m3u8 file and decrypt all .ts segments:
I understand that there may be much simpler methods than mine. However, I found that using ffmpeg to read the m3u8 file helps avoid audio and video synchronization issues. If you have a simpler method, feel free to let me know.Code:ffmpeg -allowed_extensions ALL -i list.m3u8 -c copy out.mp4
+ Reply to Thread
Results 1 to 3 of 3
-
Last edited by ipid; 12th Aug 2023 at 01:03.
-
Thanks a lot.
just for more understanding for step 2, why we should create key.key file this way? why not just create a new notepad file and paste the key there then rename it?
Asking this because I remember that I did all of that a long time ago except that I created key.key file with Notepad and it didn't work
Similar Threads
-
Decrypting a cenc encrypted video???
By leaderrocks in forum Video Streaming DownloadingReplies: 11Last Post: 9th Jun 2023, 18:32 -
How to decrypt aes 128 encrypted key???
By anandgpt75 in forum Video Streaming DownloadingReplies: 20Last Post: 26th May 2023, 17:50 -
MPD CENC Encrypted stream on ffmpeg
By Near773 in forum Video Streaming DownloadingReplies: 10Last Post: 6th Aug 2022, 08:57 -
How to get the keys to decrypt these videos?
By Hammer in forum Video Streaming DownloadingReplies: 5Last Post: 2nd Aug 2022, 16:50 -
How to decrypt AES-128 encrypted m3u8 TS files?
By canete in forum Video Streaming DownloadingReplies: 1Last Post: 2nd Jan 2020, 13:42