Java implementation of youku video address resolution sample code sharing

  • 2020-04-01 02:45:49
  • OfStack

For example, I'm going to go to http://v.youku.com/v_show/id_XNDM2Mjc0MzAw.html for this video. So what we're going to get here is the id of the video, which is the XNDM2Mjc0MzAw part, which is unique to a video, so we're going to have to use it when we parse it.

To see the streaming of youku videos, you can use firebug, a firefox plugin, to track web pages, as shown below.

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / img.jbzj.com/file_images/article/201401/20140110111714.jpg? 2014010112056 ">

You can see that there are a lot of gets, and we're looking for a GET that takes this id as an argument, and we can find this link down here
http://v.youku.com/player/getPlayList/VideoIDS/XNDM2Mjc0MzAw/timezone/+08/version/5/source/video? Ran = 3545 & amp; Password = & amp; N = 3
This is youku getting the GET of the playlist, opening its response, this is a json, and what we need is seed, streamfileids, and segs. Segs holds the segment key of the video, and streamfileids is a bunch of garbled code that needs to be decoded with seed. The following section of the downloader is taken to illustrate the decoding process. Json is parsed using json-lib-2.4-jdk15.


List<Video> videos = new ArrayList<Video>();
JSONObject data;
//Json is just the response you just got, of type String.
data = JSONObject.fromObject(json).getJSONArray("data").getJSONObject(0);
double seed = Double.valueOf(data.getString("seed"));
String title = data.getString("title");
String fileid = data.getJSONObject("streamfileids").getString("flv");//If you want to down the mp4 format
String realFileid = getFileID(fileid, seed);            //Change FLV to mp4 (provided the node exists)
String fileid1 = realFileid.substring(0, 8);//The decoded id is divided into two parts, with video segments inserted in between
String fileid2 = realFileid.substring(10);

JSONArray segs = data.getJSONObject("segs").getJSONArray("flv");//Segs partial resolution
for (Iterator iterator = segs.iterator(); iterator.hasNext();) {
    JSONObject object = (JSONObject) iterator.next();
    int order = object.getInt("no");
    String size = object.getString("size");
    int seconds = object.getInt("seconds");
    String key = object.getString("k");
    String no = String.format("%1$02x", order);
    String youUrl = "http://f.youku.com/player/getFlvPath/sid/" + "00_"
        + no + "/st/flv/fileid/" + fileid1 + no + fileid2 + "?K="
        + key;          
    videos.add(new Video(order, seconds, youUrl, size, key, title));
}

YouUrl is the video address we want. For example, the first paragraph of the video above is:

http://f.youku.com/player/getFlvPath/sid/134434081131213125530_00/st/flv/fileid/030001090050201D77EDBC04650AC2DD6027D5-ED5F-27F6-8E73-DEF478121887& K = b499f3d5df944cfc2827e2ec

The blue one is randomly generated, and you can replace it with zero zero.
FLV represents the video format to be downloaded, or mp4, if available.
The two yellow 00's are block code, hexadecimal, for example, the first segment is 00, the second segment is 01, the 15th segment is 0f.
Next up is the fileid, which needs to be cracked through seed and streamfileids, the same for every video.
The last K is in segs, you don't have to hack, every video is different.

The two decoding functions are:


private String getFileID(String fileid, double seed) {
    String mixed = getFileIDMixString(seed);
    String[] ids = fileid.split("\*");
    StringBuilder realId = new StringBuilder();
    int idx;
    for (int i = 0; i < ids.length; i++) {
        idx = Integer.parseInt(ids[i]);
        realId.append(mixed.charAt(idx));
    }
    return realId.toString();
}

private String getFileIDMixString(double seed) {
    StringBuilder mixed = new StringBuilder();
    StringBuilder source = new StringBuilder(
        "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\:._-1234567890");
    int index, len = source.length();
    for (int i = 0; i < len; ++i) {
        seed = (seed * 211 + 30031) % 65536;
        index = (int) Math.floor(seed / 65536 * source.length());
        mixed.append(source.charAt(index));
        source.deleteCharAt(index);
    }
    return mixed.toString();
}

The code will end up with a List of Video types, where the order is the number of the Video, the seconds are the length of time, the size is the length of bytes, the youUrl is the actual Video address, and the title of the Video. There are other things you can get from json.


Related articles: