斗鱼直播源提取教程

斗鱼直播算是国内最大的直播平台之一了,但是他的网页版还是一如既往的卡顿,尤其是大主播的房间,这个时候,我们只需提取出播放地址,再使用本地播放器,就可以流畅观看最高画质啦
话不多说,开始操作

抓包法

打开直播房间,F12或者鼠标右键”检查”(审查元素),如下图
斗鱼直播源提取教程-破晓BBS 专注于技术交流及资源分享的科技博客
切换到 Network 选项卡,搜索 /lapi/live/getH5Play/,可以找到一个类似于https://www.douyu.com/lapi/live/getH5Play/房间ID的请求,查看结果,格式如下:

{
    "error": 0,
    "msg": "ok",
    "data": {
        "room_id": 288016,
        "is_mixed": false,
        "mixed_live": "",
        "mixed_url": "",
        "rtmp_cdn": "cmcc",
        "rtmp_url": "https://play2.douyuscdn.com/live",
        "rtmp_live": "288016rlols5_4000p.flv?wsAuth=xxxxxxx&token=xxxx",
        "client_ip": "0.0.0.0",
        "inNA": 0,
        "rateSwitch": 1,
        "rate": 4,
        "cdnsWithName": [{
                "name": "主线路1",
                "cdn": "cmcc"
            },
            {
                "name": "主线路2",
                "cdn": "ws-h5"
            },
            {
                "name": "备用线路5",
                "cdn": "tct-h5"
            },
            {
                "name": "备用线路6",
                "cdn": "ali-h5"
            }
        ],
        "multirates": [{
                "name": "蓝光10M",
                "rate": 0,
                "highBit": 1,
                "bit": 10000
            },
            {
                "name": "蓝光4M",
                "rate": 4,
                "highBit": 1,
                "bit": 4000
            },
            {
                "name": "超清",
                "rate": 3,
                "highBit": 0,
                "bit": 2000
            },
            {
                "name": "高清",
                "rate": 2,
                "highBit": 0,
                "bit": 1200
            },
            {
                "name": "流畅",
                "rate": 1,
                "highBit": 0,
                "bit": 550
            }
        ],
        "isPassPlayer": 0,
        "eticket": null,
        "online": 0,
        "mixedCDN": "",
        "p2p": 0,
        "streamStatus": 1,
        "smt": 0,
        "p2pMeta": null,
        "p2pCid": 0,
        "p2pCids": ""
    }
}

找到参数"rtmp_live": "288016rlols5_4000p.flv,这里的288016rlols5就是房间Key
然后我们拼凑出真实地址为:http://tx2play1.douyucdn.cn/live/288016rlols5.flv?uuid=,将这个链接放到 Potplayer 等第三方播放器里就可以播放了,默认就是最高画质
斗鱼直播源提取教程-破晓BBS 专注于技术交流及资源分享的科技博客

斗鱼直播源提取教程-破晓BBS 专注于技术交流及资源分享的科技博客

脚本法

Python 运行下面脚本即可

import requests
import re
import execjs
import time
import hashlib


def get_tt():
    tt1 = str(int(time.time()))
    tt2 = str(int((time.time() * 1000)))
    today = time.strftime('%Y%m%d', time.localtime())
    return tt1, tt2, today


def get_homejs(rid):
    room_url = 'https://m.douyu.com/' + rid
    response = requests.get(url=room_url)
    pattern_real_rid = r'"rid":(\d{1,7})'
    real_rid = re.findall(pattern_real_rid, response.text, re.I)[0]
    if real_rid != rid:
        room_url = 'https://m.douyu.com/' + real_rid
        response = requests.get(url=room_url)
    homejs = ''
    pattern = r'(function ub9.*)[\s\S](var.*)'
    result = re.findall(pattern, response.text, re.I)
    str1 = re.sub(r'eval.*;}', 'strc;}', result[0][0])
    homejs = str1 + result[0][1]
    return homejs, real_rid


def get_sign(rid, post_v, tt, ub9):
    docjs = execjs.compile(ub9)
    res2 = docjs.call('ub98484234')
    str3 = re.sub(r'\(function[\s\S]*toString\(\)', '\'', res2)
    md5rb = hashlib.md5((rid + '10000000000000000000000000001501' + tt + '2501' +
                         post_v).encode('utf-8')).hexdigest()
    str4 = 'function get_sign(){var rb=\'' + md5rb + str3
    str5 = re.sub(r'return rt;}[\s\S]*', 'return re;};', str4)
    str6 = re.sub(r'"v=.*&sign="\+', '', str5)
    docjs1 = execjs.compile(str6)
    sign = docjs1.call(
        'get_sign', rid, '10000000000000000000000000001501', tt)
    return sign


def mix_room(rid):
    result1 = 'PKing'
    return result1


def get_pre_url(rid, tt):
    request_url = 'https://playweb.douyucdn.cn/lapi/live/hlsH5Preview/' + rid
    post_data = {
        'rid': rid,
        'did': '10000000000000000000000000001501'
    }
    auth = hashlib.md5((rid + str(tt)).encode('utf-8')).hexdigest()
    header = {
        'content-type': 'application/x-www-form-urlencoded',
        'rid': rid,
        'time': tt,
        'auth': auth
    }
    response = requests.post(url=request_url, headers=header, data=post_data)
    response = response.json()
    pre_url = ''
    if response.get('error') == 0:
        real_url = (response.get('data')).get('rtmp_live')
        if 'mix=1' in real_url:
            pre_url = mix_room(rid)
        else:
            pattern1 = r'^[0-9a-zA-Z]*'
            pre_url = re.search(pattern1, real_url, re.I).group()
    return pre_url


def get_sign_url(post_v, rid, tt, ub9):
    sign = get_sign(rid, post_v, tt, ub9)
    request_url = 'https://m.douyu.com/api/room/ratestream'
    post_data = {
        'v': '2501' + post_v,
        'did': '10000000000000000000000000001501',
        'tt': tt,
        'sign': sign,
        'ver': '219032101',
        'rid': rid,
        'rate': '-1'
    }
    header = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Mobile Safari/537.36'
    }
    response = requests.post(url=request_url, headers=header, data=post_data).json()
    if response.get('code') == 0:
        real_url = (response.get('data')).get('url')
        if 'mix=1' in real_url:
            result1 = mix_room(rid)
        else:
            pattern1 = r'live/(\d{1,8}[0-9a-zA-Z]+)_?[\d]{0,4}/playlist'
            result1 = re.findall(pattern1, real_url, re.I)[0]
    else:
        result1 = 0
    return result1


def get_real_url(rid):
    rid = str(rid)
    tt = get_tt()
    url = get_pre_url(rid, tt[1])
    if url:
        return "http://tx2play1.douyucdn.cn/live/" + url + ".flv?uuid="
    else:
        result = get_homejs(rid)
        real_rid = result[1]
        homejs = result[0]
        real_url = get_sign_url(tt[2], real_rid, tt[0], homejs)
        if real_url != 0:
            real_url = "http://tx2play1.douyucdn.cn/live/" + real_url + ".flv?uuid="
        else:
            real_url = '未开播'
        return real_url


def get_url_from_js(rid):
    # 从播放页源代码中直接匹配地址
    header = {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    try:
        response = requests.get('https://www.douyu.com/{}'.format(rid), headers=header).text
        real_url = re.findall(r'live/({}[\d\w]*?)_'.format(rid), response)[0]
    except:
        real_url = '直播间未开播或不存在'
    return "http://tx2play1.douyucdn.cn/live/" + real_url + ".flv?uuid="


rid = input('请输入斗鱼数字房间号:\n')
real_url = get_real_url(rid)
print('该直播间地址为:\n' + real_url)

本文系作者 @ 原创发布在 破晓BBS 专注于技术交流及资源分享的科技博客。未经许可,禁止转载。

喜欢()
评论 (0)
    热门搜索
    Ming
    努力进阶的小码农
    6 文章
    6 评论
    12 喜欢
    Top