File size: 3,608 Bytes
7b8f88d
 
bf7d327
7b8f88d
0d6128e
 
 
 
 
 
 
 
 
 
bf7d327
0d6128e
7b8f88d
 
0d6128e
bf7d327
7b8f88d
 
186440e
bf7d327
0d6128e
7b8f88d
 
0d6128e
 
 
186440e
 
0d6128e
186440e
7b8f88d
 
 
 
 
 
 
 
 
 
 
 
bf7d327
0d11e5e
 
 
 
 
7b8f88d
0d6128e
7b8f88d
 
0d6128e
7b8f88d
0d6128e
7b8f88d
 
 
 
 
 
 
0d6128e
7b8f88d
 
bf7d327
7b8f88d
 
0d6128e
7b8f88d
0d6128e
7b8f88d
 
 
0d6128e
 
 
 
 
7b8f88d
0d6128e
7b8f88d
 
bf7d327
0d11e5e
 
bf7d327
0d11e5e
7b8f88d
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import requests
import gradio as gr
from utils import download_file, extract_fst_url, API_TIKTOK, TIMEOUT, EN_US, TMP_DIR

ZH2EN = {
    "状态栏": "Status",
    "请输入抖音视频分享短链接": "Please enter TikTok video sharing short link",
    "视频下载": "Video download",
    "视频描述": "Video description",
    "解析耗时": "Parsing time",
    "作者头像": "Author avatar",
    "作者昵称": "Author nickname",
    "作者签名": "Author signature",
    "抖音无水印视频解析": "Parse TikTok video without watermark",
    "直链输出": "Direct link output",
}


def _L(zh_txt: str):
    return ZH2EN[zh_txt] if EN_US else zh_txt


# outer func
def infer(video_url: str, direct_lnk_out: bool, cache=f"{TMP_DIR}/tiktok"):
    status = "Success"
    video = parse_time = desc = avatar = author = sign = None
    try:
        if not video_url:
            raise ValueError("视频分享链接为空!")

        video_url = extract_fst_url(video_url)
        if not video_url:
            raise ValueError("请输入有效的视频分享链接!")

        response = requests.get(API_TIKTOK, params={"url": video_url}, timeout=TIMEOUT)
        response_json = response.json()
        retcode = response_json["code"]
        if retcode == 200:
            response_data = response_json["data"]
            parse_time = response_data["parse_time"]
            additional_data = response_data["additional_data"][0]
            desc = additional_data["desc"]
            avatar = additional_data["url"].split("?from=")[0]
            author = additional_data["nickname"]
            sign = additional_data["signature"]

            if direct_lnk_out:
                video = response_data["video_url"]
            else:
                video_id = response_data["play_url"].split("video_id=")[1].split("&")[0]
                video = download_file(response_data["video_url"], video_id, cache)

        else:
            raise ConnectionError(f"接口调用失败, 错误码: HTTP {retcode}")

    except Exception as e:
        status = f"视频解析失败: {e}"

    return status, video, desc, parse_time, avatar, author, sign


def tiktok_parser():
    return gr.Interface(
        fn=infer,
        inputs=[
            gr.Textbox(
                label=_L("请输入抖音视频分享短链接"),
                placeholder="https://v.douyin.com/*",
            ),
            gr.Checkbox(label=_L("直链输出"), value=False),
        ],
        outputs=[
            gr.Textbox(label=_L("状态栏"), show_copy_button=True),
            gr.Video(
                label=_L("视频下载"),
                show_download_button=True,
                show_share_button=False,
            ),
            gr.Textbox(label=_L("视频描述"), show_copy_button=True),
            gr.Textbox(label=_L("解析耗时"), show_copy_button=True),
            gr.Image(label=_L("作者头像"), show_share_button=False),
            gr.Textbox(label=_L("作者昵称"), show_copy_button=True),
            gr.TextArea(label=_L("作者签名"), show_copy_button=True),
        ],
        title=_L("抖音无水印视频解析"),
        flagging_mode="never",
        examples=[
            ["https://v.douyin.com/8FVe5DzarE0", False],
            [
                "8.20 Njc:/ [email protected] 11/03 黑塔女士举世无双!# 大黑塔 # 黑塔 # 崩坏星穹铁道 # 再创世的凯歌 # 天才俱乐部  https://v.douyin.com/8FVe5DzarE0/ 复制此链接,打开Dou音搜索,直接观看视频!",
                True,
            ],
        ],
        cache_examples=False,
    )