SoFunction
Updated on 2025-03-07

Youku real video address resolution function implemented by C# (2014 new algorithm)

Sequence: Youku has updated the algorithm several times before (a long time ago, haha...), so many blog analytical algorithms are no longer available. Many big guys have also updated new analysis methods. I am also writing an article on the analysis process here. (The language used in this article is C#)

Due to the time limit of Youku video address, the link below may have expired when you access this article. Please forgive me.

Example: /v_show/id_XNzk2NTI0MzMy.html

1: Get video

Winning the red part of the video url. A regular expression can be obtained.

Copy the codeThe code is as follows:

string getVid(string url)
{
    string strRegex = "(?<=id_)(\\w+)";
    Regex reg = new Regex(strRegex);
    Match match = (url);
    return ();
}

2: Obtain video element information

/player/getPlayList/VideoIDS/XNzk2NTI0MzMy/Pf/4/ctype/12/ev/1

Embed the aforementioned vid into the above url to access it to obtain the video information file. Because the video information is too long, I will not post all the content here. Below is a display of some important content. (Get the file as a json file, and can be parsed directly)

Copy the codeThe code is as follows:

{ "data": [ {
            "ip": 1991941296,
            "ep": "MwXRTAsbJLnb0PbJ8uJxAdSivUU11wnKXxc=",
            "segs": {
                "hd2": [
                    {
                        "no": "0",
                        "size": "34602810",
                        "seconds": 205,
                        "k": "248fe14b4c1b37302411f67a",
                        "k2": "1c8e113cecad924c5"
                    },
                    {
                        "no": "1",
                    },] }, } ],}

The content displayed above will be used later. Among them, segs contains various formats such as hd3, hd2, flv, mp4, 3gp, etc., and are divided into several segments under each format. This time, HD2 with high definition is selected (video format is flv)

3: Splicing m3u8 address

/playlist/m3u8?ctype=12&ep={0}&ev=1&keyframe=1&oip={1}&sid={2}&token={3}&type={4}&vid={5}

There are 6 parameters above, among which vid and oip have been obtained, respectively, the previous vid and ip fields in the json file, i.e. (XNzk2NTI0MzMy and 1991941296), but ep, sid, token needs to be recalculated (the ep value in the json file cannot be used directly). The type is relatively simple, I will talk about it later.

3.1 Calculate ep, sid, token

The calculation method is simply mathematical calculation, and the calculated function is given below. Three parameters can be calculated at one time. This involves Base64 encoding and decoding knowledge, click to view.

Copy the codeThe code is as follows:

private static string myEncoder(string a, byte[] c, bool isToBase64)
        {
            string result = "";
            List<Byte> bytesR = new List<byte>();
            int f = 0, h = 0, q = 0;
            int[] b = new int[256];
            for (int i = 0; i < 256; i++)
                    b[i] = i;
            while (h < 256)
            {
                f = (f + b[h] + a[h % ]) % 256;
                int temp = b[h];
                b[h] = b[f];
                b[f] = temp;
                h++;
            }
            f = 0; h = 0; q = 0;
            while (q < )
            {
                h = (h + 1) % 256;
                f = (f + b[h]) % 256;
                int temp = b[h];
                b[h] = b[f];
                b[f] = temp;
                byte[] bytes = new byte[] { (byte)(c[q] ^ b[(b[h] + b[f]) % 256]) };
                (bytes[0]);
                result += (bytes);
                q++;
            }
            if (isToBase64)
            {
                Byte[] byteR = ();
                result = Convert.ToBase64String(byteR);
            }
            return result;
        }
        public static void getEp(string vid, string ep, ref string pNew, ref string token, ref string sid)
        {
            string template1 = "becaf9be";
            string template2 = "bf7e5f01";
            byte[] bytes = Convert.FromBase64String(ep);
            ep = (bytes);
            string temp = myEncoder(template1, bytes, false);
            string[] part = ('_');
            sid = part[0];
            token = part[1];
            string whole = ("{0}_{1}_{2}", sid, vid, token);
            byte[] newbytes = (whole);
            epNew = myEncoder(template2, newbytes, true);
        }

Calculated to obtain ep, token, and sid are diaVGE+IVMwB5CXXjz8bNHi0cCEHXJZ0vESH/7YbAMZuNaHQnT/Wzw==, 4178, 441265221168712cdf4f8, respectively. Note that ep cannot be spliced ​​directly into the url at this time, so you need to do the url encoding ToUrlEncode(ep). The final ep is diaVGE%2bIVMwB5CXXjz8bNHi0cCEHXJZ0vESH%2f7YbAMZuNaHQnT%2fWzw%3d%3d

3.2 Calculate type

The Type value is closely related to the selected segs. As selected in this article, type is flv, and the following is a comparison of segs, type and clarity.

Copy the codeThe code is as follows:

"segs", "type", "clearity"
"hd3", "flv", "1080P"
"hd2", "flv", "ultra-clear"
"mp4", "mp4", "HD"
"flvhd", "flv", "HD"
"flv", "flv", "standard definition"
"3gphd", "3gp", "HD"

3.3 Splicing address

The last m3u8 address is

/playlist/m3u8?ctype=12&ep=diaVGE%2bIVMwB5CXXjz8bNHi0cCEHXJZ0vESH%2f7YbAMZuNaHQnT%2fWzw%3d%3d&ev=1&keyframe=1&oip=1991941296&sid=441265221168712cdf4f8&token=4178&type=flv&vid=XNzk2NTI0MzMy

4: Obtain the video address

After downloading the above m3u8 file, the contents are the real address, but it still needs to be processed a little. Some of the contents are as follows:

Copy the codeThe code is as follows:

#EXTM3U
#EXT-X-TARGETDURATION:12
#EXT-X-VERSION:3
#EXTINF:6,
http://59.108.137.14/696CD107FE4D821FFBF173EB3/?ts_start=0&ts_end=5.9&ts_seg_no=0&ts_keyframe=1
#EXTINF:5.533,
http://59.108.137.14/696CD107FE4D821FFBF173EB3/?ts_start=5.9&ts_end=11.433&ts_seg_no=1&ts_keyframe=1
#EXTINF:5.467,
http://59.108.137.14/696CD107FE4D821FFBF173EB3/?ts_start=11.433&ts_end=16.9&ts_seg_no=2&ts_keyframe=1
#EXTINF:9.267,

Each url only contains about 6 seconds of video, but the parameters in the url can be removed to get the actual length. However, after each item is removed, you need to merge the same url. As shown in the above list, you can get the url fragment

http://59.108.137.14/696CD107FE4D821FFBF173EB3/

Download all the url fragments in m3u8 and you're done.