mirror of
https://git.openapi.site/https://github.com/desirecore/market.git
synced 2026-12-05 21:39:23 +08:00
feat: 新增 douyin-video-download 技能 / Add douyin-video-download skill (#293)
## 可复用场景 / Reusable scenario 抖音视频无水印下载(H5 header 两段式路线):短链解析 → 两段式请求 iesdouyin 分享页取得 ttwid 并提取 videoInfoRes.play_addr → playwm 改 play 去水印 → 下载 → ffprobe 校验。免登录、免浏览器渲染、不读取用户浏览器凭据。适用于 v.douyin.com 短链、iesdouyin 分享链接或纯视频 ID 的下载/保存/去水印需求。 Download Douyin videos without watermark over the H5-header two-pass route (short-link resolve → ttwid via two-pass share-page fetch → videoInfoRes play_addr extraction → playwm→play → download → ffprobe verify). No login, no browser rendering, no user-browser credential access. ## 来源与许可 / Provenance & license - 内容为本条目原创(含 scripts/download_douyin.py,纯 Python 标准库),方法于 2026-11-17 在视频 7708667389918858496 上端到端实测通过(1280x720 / 169.18s / 15MB)。 - 许可 MIT;redistribution: allowed;stewardship: community;maintainer: yi-ge。 - 内容不含凭据、不含临时 ref;合规边界(仅下载有权保存内容)已写入正文。 ## 兼容性影响 / Compatibility - 本地技能(SKILL.md + SKILL.zh-CN.md + scripts/),无需新工具或连接;Python 3 标准库 + 可选 ffprobe 校验。 - 未登记 builtin-skills.json(按需安装即可);manifest.stats.totalSkills 149→150。 - source_hash 已按 translate.py 算法回填,en-US 译文 translated_by: ai(由 AI 译自 zh-CN 源文,已人工复核事实一致性)。 ## 已执行校验 / Checks run - `uv run scripts/i18n/validate-i18n.py skills/douyin-video-download` → 0 error(s) - `uv run scripts/catalog/validate_catalog_metadata.py --require-complete` → 0 error(s)(publishableSkills=150) - `uv run scripts/i18n/translate.py --check skills/douyin-video-download` → up-to-date (hash match) - 脚本端到端实测:ffprobe 校验通过,与页面 duration 一致 Co-authored-by: mashagua <993498039@qq.com>
This commit is contained in:
1 parent
99153a097c
commit
7d92094b97
5 files changed
+465
-1
No files matched your search
@@ -0,0 +1,139 @@
|
||||
#!/usr/bin/env python3
|
||||
"""抖音视频无水印下载(H5 header 路线)。
|
||||
|
||||
用法:
|
||||
python3 download_douyin.py "https://v.douyin.com/<short_id>/" -o out_dir/
|
||||
python3 download_douyin.py 7708667389918858496 -o out_dir/
|
||||
|
||||
原理: 短链解析 -> ttwid cookie -> iPhone UA + Referer 请求 iesdouyin 分享页
|
||||
-> videoInfoRes.item_list[0].video.play_addr.url_list -> playwm 改 play -> 下载 -> 校验。
|
||||
"""
|
||||
import argparse
|
||||
import http.cookiejar
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import urllib.request
|
||||
|
||||
H5UA = (
|
||||
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) "
|
||||
"AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 "
|
||||
"Mobile/15E148 Safari/604.1"
|
||||
)
|
||||
REFERER = "https://www.iesdouyin.com/"
|
||||
|
||||
|
||||
def _opener(cj: http.cookiejar.CookieJar):
|
||||
return urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
|
||||
|
||||
|
||||
HEADERS = {
|
||||
"User-Agent": H5UA,
|
||||
"Referer": REFERER,
|
||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||
"Accept-Language": "zh-CN,zh;q=0.9",
|
||||
}
|
||||
|
||||
|
||||
def fetch(url: str, cj: http.cookiejar.CookieJar, timeout: int = 60) -> bytes:
|
||||
req = urllib.request.Request(url, headers=HEADERS)
|
||||
with _opener(cj).open(req, timeout=timeout) as resp:
|
||||
return resp.read()
|
||||
|
||||
|
||||
def resolve_video_id(source: str, cj: http.cookiejar.CookieJar) -> str:
|
||||
if re.fullmatch(r"\d{15,25}", source):
|
||||
return source
|
||||
req = urllib.request.Request(source, headers={"User-Agent": H5UA})
|
||||
with _opener(cj).open(req, timeout=30) as resp:
|
||||
final = resp.geturl()
|
||||
m = re.search(r"/(?:share/)?video/(\d+)", final)
|
||||
if not m:
|
||||
raise SystemExit(f"无法从跳转地址解析 video_id: {final}")
|
||||
return m.group(1)
|
||||
|
||||
|
||||
def extract_play_urls(html: str):
|
||||
"""返回 (播放地址列表[已去水印], 页面duration数值或None)。"""
|
||||
i = html.find('"videoInfoRes":{')
|
||||
if i < 0:
|
||||
raise SystemExit(
|
||||
"页面无 videoInfoRes:请确认同时提供了 iPhone H5 UA、"
|
||||
"Referer: https://www.iesdouyin.com/ 与 ttwid cookie(三要素缺一不可)"
|
||||
)
|
||||
block = html[i:]
|
||||
m = re.search(r'"play_addr":\{"uri":"[^"]*","url_list":(\[.*?\])', block)
|
||||
if not m:
|
||||
raise SystemExit("videoInfoRes 中未找到 play_addr.url_list")
|
||||
urls = json.loads(m.group(1).replace("\\u002F", "/"))
|
||||
dur = re.search(r'"duration":(\d+)', block)
|
||||
urls = [u.replace("playwm", "play") for u in urls]
|
||||
return urls, (int(dur.group(1)) if dur else None)
|
||||
|
||||
|
||||
def verify(path: str) -> None:
|
||||
ffprobe = shutil.which("ffprobe") or os.environ.get("FFPROBE") or (
|
||||
"/Users/yige/.desirecore/runtime/toolkits/ffmpeg/7.1.5-dc1/bin/ffprobe"
|
||||
if os.name == "darwin" else ""
|
||||
)
|
||||
if not ffprobe or not os.path.exists(ffprobe):
|
||||
print("ffprobe 不可用,跳过元数据校验(文件已下载)")
|
||||
return
|
||||
out = subprocess.run(
|
||||
[ffprobe, "-v", "error", "-show_entries",
|
||||
"format=duration,size:stream=codec_type,codec_name,width,height",
|
||||
"-of", "default=noprint_wrappers=1", path],
|
||||
capture_output=True, text=True,
|
||||
)
|
||||
print("=== ffprobe ===")
|
||||
print(out.stdout.strip())
|
||||
|
||||
|
||||
def main() -> None:
|
||||
ap = argparse.ArgumentParser(description="抖音视频无水印下载(H5 header 路线)")
|
||||
ap.add_argument("source", help="抖音短链 / 分享链接 / 纯 video_id")
|
||||
ap.add_argument("-o", "--out", default=".", help="输出目录(默认当前目录)")
|
||||
ap.add_argument("-n", "--name", default=None, help="输出文件名(默认 <video_id>.mp4)")
|
||||
args = ap.parse_args()
|
||||
|
||||
cj = http.cookiejar.CookieJar()
|
||||
try: # ttwid 是设备会话 cookie,不是登录凭据
|
||||
fetch("https://www.douyin.com/", cj)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
vid = resolve_video_id(args.source, cj)
|
||||
print(f"video_id: {vid}")
|
||||
share_url = f"https://www.iesdouyin.com/share/video/{vid}/"
|
||||
html = fetch(share_url, cj).decode("utf-8", "replace")
|
||||
if '"videoInfoRes"' not in html:
|
||||
# iesdouyin 首次响应只下发 ttwid;带 cookie 二次请求才返回 videoInfoRes
|
||||
html = fetch(share_url, cj).decode("utf-8", "replace")
|
||||
urls, duration = extract_play_urls(html)
|
||||
if duration:
|
||||
print(f"页面 duration: {duration}(注意单位,可能是毫秒)")
|
||||
|
||||
os.makedirs(args.out, exist_ok=True)
|
||||
out_path = os.path.join(args.out, args.name or f"{vid}.mp4")
|
||||
for u in urls:
|
||||
print(f"尝试播放地址: {u[:100]}...")
|
||||
try:
|
||||
data = fetch(u, cj)
|
||||
if len(data) < 10_000:
|
||||
print(" 返回过小,判定无效,换下一个", file=sys.stderr)
|
||||
continue
|
||||
with open(out_path, "wb") as f:
|
||||
f.write(data)
|
||||
print(f"下载完成: {out_path} ({len(data)} bytes)")
|
||||
verify(out_path)
|
||||
return
|
||||
except Exception as exc: # noqa: BLE001
|
||||
print(f" 失败: {exc}", file=sys.stderr)
|
||||
raise SystemExit("所有播放地址均下载失败")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user