VideoHelp Forum
+ Reply to Thread
Results 1 to 3 of 3
Thread
  1. 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:

    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"
    This indicates that your video is encrypted using the SAMPLE-AES-CTR algorithm and the IV value is 0x62bdf8c903e29da1e690cde43ca18ac6.

    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:
    Image
    [Attachment 71273 - Click to enlarge]


    Then, I will paste this key into the file, select "Overwrite the bytes", and choose "Hexadecimal Values":
    Image
    [Attachment 71274 - Click to enlarge]


    Finally, save the file as key.key:
    Image
    [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:

    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
    The key points of this file are:
    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:

    Code:
    ffmpeg -allowed_extensions ALL -i list.m3u8 -c copy out.mp4
    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.
    Last edited by ipid; 12th Aug 2023 at 01:03.
    Quote Quote  
  2. Member
    Join Date
    Feb 2022
    Location
    Search the forum first!
    Search PM
    Informative. Thanks for sharing.
    Quote Quote  
  3. 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
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!