How to Reverse Engineer JS Video Encryption with Python: A Step‑by‑Step Guide

This article walks through a real‑world case of reverse‑engineering a JavaScript video‑encryption routine, translating its core functions into Python, and demonstrates how to reconstruct the encrypted URL to download the video using a web‑scraping approach.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Reverse Engineer JS Video Encryption with Python: A Step‑by‑Step Guide

Preface

The author, a Python enthusiast, noticed several community members asking for JavaScript reverse‑engineering tutorials and decided to share a practical example that converts a JS video‑encryption routine into Python.

JS Code

The target JavaScript code, extracted from a small video‑hosting site, defines a decodeMp4.decode() function that relies on helper functions such as getHex, getDec, substr, and getPos. The original script is shown below:

define("tool", function(a, b, c) {
    var d = a("jquery"),
        e = a("support"),
        f = a("constants"),
        g = a("base64"),
        h = "substring",
        i = "split",
        j = "replace",
        k = "substr";
    b.decodeMp4 = {
        getHex: function(a) {
            return { str: a[h](4), hex: a[h](0, 4)[i]("").reverse().join("") };
        },
        getDec: function(a) {
            var b = parseInt(a, 16).toString();
            return { pre: b[h](0, 2)[i](""), tail: b[h](2)[i]("") };
        },
        substr: function(a, b) {
            var c = a[h](0, b[0]), d = a[k](b[0], b[1]);
            return c + a[h](b[0])[j](d, "");
        },
        getPos: function(a, b) {
            return b[0] = a.length - b[0] - b[1], b;
        },
        decode: function(a) {
            var b = this.getHex(a), c = this.getDec(b.hex), d = this[k](b.str, c.pre);
            return g.atob(this[k](d, this.getPos(d, c.tail)));
        }
    };
});

An accompanying screenshot of the function on the original page is included below:

Conversion Process

The variable a represents a long encrypted string obtained via breakpoint debugging. An example value is shown:

a = "c0b1Ly9tdnPflQ3cQpPZpZGVvMTAubWVpdHVkYXRhLmNvbS82MWM0NDNlOGI1MmFmMTYzMi5tcDkBOyQ"

1. getHex(a) – JavaScript → Python

def getHex(a):
    return {
        "str": a[4:],  # substring(4) in JS
        "hex": "".join(list(a[0:4])[::-1])  # reverse the first 4 chars
    }

2. getDec(a) – JavaScript → Python

def getDec(a):
    b = str(int(a, 16))
    return {
        "pre": list(b[:2]),
        "tail": list(b[2:])
    }

3. substr(a, b) – JavaScript → Python

def substr(a, b):
    c = a[0:int(b[0])]
    d = a[int(b[0]):int(b[0]) + int(b[1])]
    return c + a[int(b[0]):].replace(d, "")

4. getPos(a, b) – JavaScript → Python

def getPos(a, b):
    b[0] = len(a) - int(b[0]) - int(b[1])
    return b

5. decode(a) – JavaScript → Python

def decode(a):
    b = getHex(a)
    c = getDec(b["hex"])
    d = substr(b["str"], c["pre"])
    return base64.b64decode(substr(d, getPos(d, c["tail"])) )

Effect Demonstration

Running the translated Python code reproduces the same encrypted video URL that the original webpage generates. The resulting URL can be opened in a browser to stream the video, and a direct download request retrieves the video file. Screenshots of the successful decryption are shown below:

Summary

The article demonstrates a complete workflow for handling JavaScript‑based video encryption in Python web‑scraping scenarios: extracting the obfuscated string, translating the JS helper functions into Python, and finally reconstructing the real video URL for playback or download.

pythonprogrammingweb-scrapingCode TranslationVideo DecryptionJS reverse engineering
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.