Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

抖音好像不行啦,获取real url失败 #447

Open
oiuv opened this issue Aug 15, 2023 · 14 comments
Open

抖音好像不行啦,获取real url失败 #447

oiuv opened this issue Aug 15, 2023 · 14 comments

Comments

@oiuv
Copy link

oiuv commented Aug 15, 2023

请输入抖音直播链接或19位room_id:7267533488792980261
room_id 7267533488792980261
获取real url失败

@shixiuhai
Copy link

class DouYin:
def init(self):
pass

def get_real_url(self,rid:str)->dict:
    url = 'https://live.douyin.com/{}'.format(rid)
    headers = {
        "cookie": "__ac_nonce=000000000000000000000; ",
        # "cookie": "__ac_nonce=063e4d7460011686f2fcf; ",
        "referer": "https://live.douyin.com/",
        "upgrade-insecure-requests": "1",
        "user-agent": "Mozilla/5.0(WindowsNT10.0;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/86.0.4240.198Safari/537.36",
    }
    # s = requests.session()
    response = requests.get(url, headers=headers)

    text = urllib.parse.unquote(re.findall('<script id="RENDER_DATA" type="application/json">(.*?)</script>', response.text)[0])

    json_ = json.loads(text)
    try:
        # 主播昵称
        name = json_['app']['initialState']['roomStore']['roomInfo']['room']['owner']['nickname']
        roomId=rid
        flv_pull_url = json_['app']['initialState']['roomStore']['roomInfo']['room']['stream_url']['flv_pull_url']["SD2"]
        # hls_pull_url_map = json_['app']['initialState']['roomStore']['roomInfo']['room']['stream_url']['hls_pull_url_map']
        return {
            "name":name,
            "flvUrl":flv_pull_url,
            "roomId":roomId
        }
    except Exception as error:
        return None

@oiuv
Copy link
Author

oiuv commented Aug 28, 2023

class DouYin:
def init(self):
pass

谢谢,搞定。

@oiuv
Copy link
Author

oiuv commented Sep 10, 2023

上面的又不行了,最新有效代码如下:

import requests
import re
import json


class Douyin:

    def __init__(self, rid, stream):
        self.Rid = rid
        self.Stream = stream

    def get_douyin_url(self):
        live_url = f"https://live.douyin.com/{self.Rid}"
        session = requests.Session()

        # Send initial request to obtain __ac_nonce
        headers = {
            "User-Agent":
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
            "Upgrade-Insecure-Requests": "1",
        }
        oresp = session.get(live_url, headers=headers)
        oresp.close()

        # Extract __ac_nonce from Set-Cookie header
        ac_nonce_match = re.search(r'(?i)__ac_nonce=(.*?);', oresp.headers.get("Set-Cookie", ""))
        if ac_nonce_match:
            ac_nonce = ac_nonce_match.group(1)
        else:
            return None

        # Set __ac_nonce cookie and send another request
        session.cookies.set("__ac_nonce", ac_nonce)
        resp = session.get(live_url, headers=headers)

        # Extract ttwid from Set-Cookie header
        ttwid_match = re.search(r'(?i)ttwid=.*?;', resp.headers.get("Set-Cookie", ""))
        if ttwid_match:
            ttwid = ttwid_match.group(0)
        else:
            return None

        # Build URL for final request
        url = f"https://live.douyin.com/webcast/room/web/enter/?aid=6383&app_name=douyin_web&live_id=1&device_platform=web&language=zh-CN&enter_from=web_live&cookie_enabled=true&screen_width=1728&screen_height=1117&browser_language=zh-CN&browser_platform=MacIntel&browser_name=Chrome&browser_version=116.0.0.0&web_rid={self.Rid}"
        headers = {
            "User-Agent":
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
            "Cookie": ttwid,
            "Accept": "*/*",
            "Host": "live.douyin.com",
            "Connection": "keep-alive",
        }

        # Send the final request
        ress = session.get(url, headers=headers)
        ress.close()

        # Parse the JSON response
        data = json.loads(ress.text)
        status = data.get("data", {}).get("data", [])[0].get("status", 0)

        if status != 2:
            return None

        real_url = ""
        stream_data = data.get("data", {}).get("data", [])[0].get("stream_url", {}).get("live_core_sdk_data", {}).get("pull_data", {}).get("stream_data", {})

        value = json.loads(stream_data)
        if self.Stream == "flv":
            real_url = value.get("data", {}).get("origin", {}).get("main", {}).get("flv", "")
        elif self.Stream == "hls":
            real_url = value.get("data", {}).get("origin", {}).get("main", {}).get("hls", "")

        return real_url


def main():
    room_url = input("请输入抖音直播间URL: ")
    quality = "flv"  # 或者根据需要设置hls
    # Extract room ID from URL
    match = re.search(r'https://live.douyin.com/(\d+)', room_url)
    if not match:
        return None
    room_id = match.group(1)
    douyin_obj = Douyin(room_id, quality)
    douyin_url = douyin_obj.get_douyin_url()

    print(douyin_url)


if __name__ == "__main__":
    main()

@sbwml
Copy link

sbwml commented Oct 12, 2023

@oiuv 手机客户端分享出来的直播链接有办法吗?

像这种:https://v.douyin.com/idSEom1x/

2- #在抖音,记录美好生活#【B太】正在直播,来和我一起支持Ta吧。复制下方链接,打开【抖音】,直接观看直播! https://v.douyin.com/idSEom1x/ 4@7.com 02/14

@zhzzzzzz66
Copy link

class DouYin: def init(self): pass

def get_real_url(self,rid:str)->dict:
    url = 'https://live.douyin.com/{}'.format(rid)
    headers = {
        "cookie": "__ac_nonce=000000000000000000000; ",
        # "cookie": "__ac_nonce=063e4d7460011686f2fcf; ",
        "referer": "https://live.douyin.com/",
        "upgrade-insecure-requests": "1",
        "user-agent": "Mozilla/5.0(WindowsNT10.0;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/86.0.4240.198Safari/537.36",
    }
    # s = requests.session()
    response = requests.get(url, headers=headers)

    text = urllib.parse.unquote(re.findall('<script id="RENDER_DATA" type="application/json">(.*?)</script>', response.text)[0])

    json_ = json.loads(text)
    try:
        # 主播昵称
        name = json_['app']['initialState']['roomStore']['roomInfo']['room']['owner']['nickname']
        roomId=rid
        flv_pull_url = json_['app']['initialState']['roomStore']['roomInfo']['room']['stream_url']['flv_pull_url']["SD2"]
        # hls_pull_url_map = json_['app']['initialState']['roomStore']['roomInfo']['room']['stream_url']['hls_pull_url_map']
        return {
            "name":name,
            "flvUrl":flv_pull_url,
            "roomId":roomId
        }
    except Exception as error:
        return None

大哥 有能获取高画质的吗

@RallyQ
Copy link

RallyQ commented Apr 5, 2024

上面的又不行了,最新有效代码如下:

import requests
import re
import json


class Douyin:

    def __init__(self, rid, stream):
        self.Rid = rid
        self.Stream = stream

    def get_douyin_url(self):
        live_url = f"https://live.douyin.com/{self.Rid}"
        session = requests.Session()

        # Send initial request to obtain __ac_nonce
        headers = {
            "User-Agent":
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
            "Upgrade-Insecure-Requests": "1",
        }
        oresp = session.get(live_url, headers=headers)
        oresp.close()

        # Extract __ac_nonce from Set-Cookie header
        ac_nonce_match = re.search(r'(?i)__ac_nonce=(.*?);', oresp.headers.get("Set-Cookie", ""))
        if ac_nonce_match:
            ac_nonce = ac_nonce_match.group(1)
        else:
            return None

        # Set __ac_nonce cookie and send another request
        session.cookies.set("__ac_nonce", ac_nonce)
        resp = session.get(live_url, headers=headers)

        # Extract ttwid from Set-Cookie header
        ttwid_match = re.search(r'(?i)ttwid=.*?;', resp.headers.get("Set-Cookie", ""))
        if ttwid_match:
            ttwid = ttwid_match.group(0)
        else:
            return None

        # Build URL for final request
        url = f"https://live.douyin.com/webcast/room/web/enter/?aid=6383&app_name=douyin_web&live_id=1&device_platform=web&language=zh-CN&enter_from=web_live&cookie_enabled=true&screen_width=1728&screen_height=1117&browser_language=zh-CN&browser_platform=MacIntel&browser_name=Chrome&browser_version=116.0.0.0&web_rid={self.Rid}"
        headers = {
            "User-Agent":
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
            "Cookie": ttwid,
            "Accept": "*/*",
            "Host": "live.douyin.com",
            "Connection": "keep-alive",
        }

        # Send the final request
        ress = session.get(url, headers=headers)
        ress.close()

        # Parse the JSON response
        data = json.loads(ress.text)
        status = data.get("data", {}).get("data", [])[0].get("status", 0)

        if status != 2:
            return None

        real_url = ""
        stream_data = data.get("data", {}).get("data", [])[0].get("stream_url", {}).get("live_core_sdk_data", {}).get("pull_data", {}).get("stream_data", {})

        value = json.loads(stream_data)
        if self.Stream == "flv":
            real_url = value.get("data", {}).get("origin", {}).get("main", {}).get("flv", "")
        elif self.Stream == "hls":
            real_url = value.get("data", {}).get("origin", {}).get("main", {}).get("hls", "")

        return real_url


def main():
    room_url = input("请输入抖音直播间URL: ")
    quality = "flv"  # 或者根据需要设置hls
    # Extract room ID from URL
    match = re.search(r'https://live.douyin.com/(\d+)', room_url)
    if not match:
        return None
    room_id = match.group(1)
    douyin_obj = Douyin(room_id, quality)
    douyin_url = douyin_obj.get_douyin_url()

    print(douyin_url)


if __name__ == "__main__":
    main()

有效,感谢!@oiuv
能否获取到最低清晰度视频链接?怎样改?要不时间长的话文件体积太大。

@shixiuhai
Copy link

个人觉得应该有两种方案一种在这里 stream_data = data.get("data", {}).get("data", [])[0].get("stream_url", {}).get("live_core_sdk_data", {}).get("pull_data", {}).get("stream_data", {})打印data看看里面有没有分辨率更低的流进行切换,另一种使用opencv读取最后获取的flv流并进行cv2.resize()来强制降低分辨率

@RallyQ
Copy link

RallyQ commented Apr 6, 2024

个人觉得应该有两种方案一种在这里 stream_data = data.get("data", {}).get("data", [])[0].get("stream_url", {}).get("live_core_sdk_data", {}).get("pull_data", {}).get("stream_data", {})打印data看看里面有没有分辨率更低的流进行切换,另一种使用opencv读取最后获取的flv流并进行cv2.resize()来强制降低分辨率

多谢指导!
打印了data,查看参数找到了清晰度的标记,原来有四种,最高清晰度是origin,之后是sd ld md,md是最低清
只要更改real_url = value.get行,讲origin换成ld,就符合我的要求了

@RallyQ
Copy link

RallyQ commented Apr 6, 2024

@shixiuhai 你好!再请教一下
live_url = f"https://live.douyin.com/{self.Rid}"
此行能否改成兼容py3.6以下的格式?怎样改?
查了一下说f字符串是3.6版之后新增的,旧版py不兼容此写法

@shixiuhai
Copy link

列举两种替换方案方案一: live_url = "https://live.douyin.com/{}".format(self.Rid) 方案二live_url = "https://live.douyin.com/%s" % self.Rid

@shixiuhai
Copy link

介于gpt是免费的,这类基础语法替换你可以尝试gpt https://chat.openai.com/ 或者文心一言讯飞这些都是可以直接获取答案的

@RallyQ
Copy link

RallyQ commented Apr 7, 2024

@shixiuhai 高手啊!两种方案都有效,感谢你的热心指导!

@RallyQ
Copy link

RallyQ commented Apr 10, 2024

@shixiuhai 有没有快手的类似功能的py代码?网上找半天,没找到正常可用的

@RallyQ
Copy link

RallyQ commented May 21, 2024

有没有获取非直播间的普通抖音视频实际下载链接的脚本?能选择视频清晰度
找了几个抖音下载工具,都是直接下载最高清视频,无法选清晰度
有的长视频,1080P文件体积太大,没必要

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants