首先是通过 F12 拿到 AID 和 CID(见其它文章),然后调用这个接口:
https://api.bilibili.com/x/player/wbi/v2?aid={av}&cid={cid}
{"code":0,"message":"0","ttl":1,"data":{"aid":1855215937,"bvid":"BV1Rs421u7C4","allow_bp":false,"no_share":false,"cid":1560156670,"max_limit":3000,"page_no":1,"has_next":false,"ip_info":{"ip":"124.64.124.137","zone_ip":" 10.66.22.34","zone_id":4243456,"country":"中国","province":"北京","city":""},"login_mid":9169694,"login_mid_hash":"c325f1d7","is_owner":false,"name":"绝不原创的飞龙","permission":"10000,1001","level_info":{"current_level":6,"current_min":28800,"current_exp":33049,"next_exp":-1,"level_up":1695833946},"vip":{"type":2,"status":1,"due_date":1730649600000,"vip_pay_type":1,"theme_type":0,"label":{"path":"","text":"年度大会员","label_theme":"annual_vip","text_color":"#FFFFFF","bg_style":1,"bg_color":"#FB7299","border_color":"","use_img_label":true,"img_label_uri_hans":"","img_label_uri_hant":"","img_label_uri_hans_static":"https://i0.hdslb.com/bfs/vip/8d4f8bfc713826a5412a0a27eaaac4d6b9ede1d9.png","img_label_uri_hant_static":"https://i0.hdslb.com/bfs/activity-plat/static/20220614/e369244d0b14644f5e1a06431e22a4d5/VEW8fCC0hg.png"},"avatar_subscript":1,"nickname_color":"#FB7299","role":3,"avatar_subscript_url":"","tv_vip_status":0,"tv_vip_pay_type":0,"tv_due_date":0,"avatar_icon":{"icon_type":1,"icon_resource":{}}},"answer_status":0,"block_time":0,"role":"0","last_play_time":-1000,"last_play_cid":1560156670,"now_time":1726370540,"online_count":1,"need_login_subtitle":false,"subtitle":{"allow_submit":false,"lan":"","lan_doc":"","subtitles":[{"id":1497922385058359296,"lan":"ai-zh","lan_doc":"中文(自动生成)","is_lock":false,"subtitle_url":"//aisubtitle.hdslb.com/bfs/ai_subtitle/prod/18552159371560156670a544e45bb1c1fcbc749444766bcfdce1?auth_key=1726370540-6200821378ad42a7a48c21fe4b226486-0-5b557de4cbb342c3e46e5f78c068b28f","type":1,"id_str":"1497922385058359296","ai_type":0,"ai_status":2}]},"view_points":[],"preview_toast":"为创作付费,购买观看完整视频|购买观看","options":{"is_360":false,"without_vip":false},"guide_attention":[{"type":1,"from":66,"to":71,"pos_x":333.5,"pos_y":243.75}],"jump_card":[],"operation_card":[],"online_switch":{"enable_gray_dash_playback":"500","new_broadcast":"1","realtime_dm":"1","subtitle_submit_switch":"1"},"fawkes":{"config_version":30787,"ff_version":21289},"show_switch":{"long_progress":false},"bgm_info":null,"toast_block":false,"is_upower_exclusive":false,"is_upower_play":false,"is_ugc_pay_preview":false,"elec_high_level":{"privilege_type":0,"title":"","sub_title":"","show_button":false,"button_text":"","jump_url":"","intro":"","new":false},"disable_show_up_info":false}}
它的data.subtitle.subtitles
就是字幕链接,这个需要 COOKIE,否则为空。
j = requests.get(url, headers=hdrs).json()
subtitles = j['data']['subtitle']['subtitles']
prefs = [s for s in subtitles if s['lan'] in ['ai-zh', 'zh']]
if not prefs:
print(f'{fname} 无可用字幕')
return
url = 'https:' + prefs[0]['subtitle_url']
sub = requests.get(url, headers=hdrs).json()
提取出来的 JSON 格式是这样:
{
"font_size": 0.4,
"font_color": "#FFFFFF",
"background_alpha": 0.5,
"background_color": "#9C27B0",
"Stroke": "none",
"type": "AIsubtitle",
"lang": "zh",
"version": "v1.7.0.4",
"body": [
{
"from": 1.86,
"to": 4.46,
"sid": 1,
"location": 2,
"content": "那你们有没有背着露露",
"music": 0.0
},
{
"from": 5.82,
"to": 13.25,
"sid": 2,
"location": 2,
"content": "偷偷的偷偷的看一些不该看的东西呢",
"music": 0.0
},
{
"from": 15.759,
"to": 17.999,
"sid": 3,
"location": 2,
"content": "啊我猜一定有吧",
"music": 0.0
},
{
"from": 19.319,
"to": 23.999,
"sid": 4,
"location": 2,
"content": "是不是有就没有背着我偷偷看些什么吗",
"music": 0.0
},
{
"from": 32.0,
"to": 35.0,
"sid": 5,
"location": 2,
"content": "可是你们的房间",
"music": 0.0
},
<...>
]
}
写个代码转 SRT:
def float2hhmmss(num):
int_ = int(num)
frac = int((num - int_) * 1000)
hr, min_, sec = int_ // 3600, int_ % 3600 // 60, int_ % 60
return f'{hr}:{min_:02d}:{sec:02d}.{frac:03d}'
def bilisub2srt(j):
subs = j['body']
srts = []
for i, sub in enumerate(subs, start=1):
st = float2hhmmss(sub['from'])
ed = float2hhmmss(sub['to'])
txt = sub['content']
srtpt = f'{i}\n{st} --> {ed}\n{txt}'
srts.append(srtpt)
srt = '\n\n'.join(srts)
return srt
结果:
1
0:00:01.860 --> 0:00:04.459
那你们有没有背着露露
2
0:00:05.820 --> 0:00:13.250
偷偷的偷偷的看一些不该看的东西呢
3
0:00:15.759 --> 0:00:17.998
啊我猜一定有吧
4
0:00:19.318 --> 0:00:23.998
是不是有就没有背着我偷偷看些什么吗
5
0:00:32.000 --> 0:00:35.000
可是你们的房间
<...>
标签:教程,false,vip,00,label,BILIBILI,字幕,subtitle,type
From: https://www.cnblogs.com/apachecn/p/18415093