e0a2b323d9
- obsidian-kb: Add OCR processors (easyocr, tesseract) - obsidian-kb: Fix subtitle detection with test files - obsidian-kb: Add cookie verification script - Add find-skills skill for discovering capabilities - Add video processing scripts - Update clawhub lock file
78 lines
3.3 KiB
Python
78 lines
3.3 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
测试B站字幕提取问题
|
|
"""
|
|
import asyncio
|
|
import json
|
|
from playwright.async_api import async_playwright
|
|
|
|
async def test_subtitle_api(bvid):
|
|
"""测试字幕API"""
|
|
url = f"https://www.bilibili.com/video/{bvid}"
|
|
|
|
async with async_playwright() as playwright:
|
|
browser = await playwright.chromium.launch(headless=True)
|
|
context = await browser.new_context()
|
|
page = await context.new_page()
|
|
|
|
await page.goto(url, wait_until='domcontentloaded')
|
|
await asyncio.sleep(2)
|
|
|
|
# 方法1: 获取 aid
|
|
print("=== 测试方法1: 使用 aid ===")
|
|
try:
|
|
aid = await page.evaluate('() => window.__INITIAL_STATE__?.videoData?.aid')
|
|
print(f"aid: {aid}")
|
|
if aid:
|
|
api_url = "https://api.bilibili.com/x/player/v2"
|
|
response = await page.request.get(api_url, params={'aid': aid, 'bvid': bvid})
|
|
data = await response.json()
|
|
print(f"Response code: {data.get('code')}")
|
|
print(f"Subtitle info: {json.dumps(data.get('data', {}).get('subtitle', {}), indent=2, ensure_ascii=False)}")
|
|
except Exception as e:
|
|
print(f"方法1失败: {e}")
|
|
|
|
# 方法2: 获取 cid
|
|
print("\n=== 测试方法2: 使用 cid ===")
|
|
try:
|
|
cid = await page.evaluate('() => window.__INITIAL_STATE__?.videoData?.cid')
|
|
print(f"cid: {cid}")
|
|
if cid:
|
|
api_url = "https://api.bilibili.com/x/player/v2"
|
|
response = await page.request.get(api_url, params={'bvid': bvid, 'cid': cid})
|
|
data = await response.json()
|
|
print(f"Response code: {data.get('code')}")
|
|
print(f"Subtitle info: {json.dumps(data.get('data', {}).get('subtitle', {}), indent=2, ensure_ascii=False)}")
|
|
|
|
# 如果有字幕,尝试获取字幕内容
|
|
subtitle_info = data.get('data', {}).get('subtitle', {})
|
|
subtitles_list = subtitle_info.get('subtitles', [])
|
|
if subtitles_list:
|
|
subtitle_url = subtitles_list[0].get('subtitle_url', '')
|
|
print(f"\nSubtitle URL: {subtitle_url}")
|
|
full_url = f"https:{subtitle_url}"
|
|
response = await page.request.get(full_url)
|
|
if response.ok:
|
|
sub_data = await response.json()
|
|
print(f"First 3 subtitles:")
|
|
for i, item in enumerate(sub_data.get('body', [])[:3]):
|
|
print(f" [{i+1}] {item}")
|
|
except Exception as e:
|
|
print(f"方法2失败: {e}")
|
|
|
|
# 方法3: 从页面直接提取
|
|
print("\n=== 测试方法3: 从页面直接提取 ===")
|
|
try:
|
|
subtitle_data = await page.evaluate('() => window.__INITIAL_STATE__?.videoData?.subtitle')
|
|
print(f"Subtitle data from page: {json.dumps(subtitle_data, indent=2, ensure_ascii=False)}")
|
|
except Exception as e:
|
|
print(f"方法3失败: {e}")
|
|
|
|
await browser.close()
|
|
|
|
if __name__ == '__main__':
|
|
# 测试一个有字幕的视频
|
|
bvid = "BV1MnFNz5EDL" # 可以换成其他视频
|
|
asyncio.run(test_subtitle_api(bvid))
|