首页 > 其他分享 >BILIBILI 字幕提取教程简略版

BILIBILI 字幕提取教程简略版

时间:2024-09-15 11:24:02浏览次数:8  
标签:教程 false vip 00 label BILIBILI 字幕 subtitle type

首先是通过 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

相关文章

  • 蓝易云服务器 - Ubuntu16.04安装VSCode教程
    安装VSCode的教程如下:打开终端,运行以下命令更新软件包列表:sudoaptupdate安装依赖库:sudoaptinstallsoftware-properties-commonapt-transport-httpswget导入MicrosoftGPG密钥:wget-qhttps://packages.microsoft.com/keys/microsoft.asc-O-|sudoapt-keyadd-添加Visual......
  • 蓝易云服务器 - ubuntu安装开发javascript ubuntu script教程
    在Ubuntu上安装开发JavaScript的教程如下:打开终端。安装Node.js:运行以下命令安装Node.js。sudoaptupdatesudoaptinstallnodejs安装npm:npm是Node.js的包管理器,运行以下命令安装npm。sudoaptinstallnpm验证安装:通过运行以下命令验证Node.js和npm是否安装成功。node-vnpm-......
  • 【数据库系统实用教程】 第一章 数据库系统概述
    1.1基本概念1.数据什么是数据:数据是现实世界中客体在计算机中的抽象表示,具体的说,它是一种存储于计算机内的符号串。数据的特性:(1)数据表现形式的多样性(2)数据的可构造性:数据分为结构化数据、半结构化数据和非结构化数据。结构化数据有型(type)和值(value)之分。结构化数......
  • 《在 Windows 系统中配置 Python 3.11 环境安装教程》
    一、引言Python是一种广泛使用的高级编程语言,具有简洁易读、可扩展性强等优点。Python3.11是Python的一个重要版本,带来了许多新的特性和改进。本文将详细介绍在Windows系统中配置Python3.11环境的步骤,帮助读者快速搭建开发环境。二、安装前的准备(一)确认系统要......
  • Linkedin-SRE-中文教程-二-
    LinkedinSRE中文教程(二)原文:SchoolofSRE协议:CCBY-NC-SA4.0一些Python概念原文:https://linkedin.github.io/school-of-sre/level101/python_web/python-concepts/虽然期望您了解python及其基本语法,但是让我们讨论一些基本概念,这将帮助您更好地理解python语言。......
  • 最新! Sagemath Windows 平台最新安装教程 2024
    SageMath安装指南:Windows平台最新安装方法(Sage版本为最新10.4)本文虽然为AI写的(还只有文字),但是截止发文的日期,本人并未在中文互联网上搜索到如何安装sagemath的最新版10.4。所有的方法中,需要Cygwin的exe直装的Sagemath9.3版的;而ubuntu直接apt安装的Sagemath版本为9.5(如果是20.......
  • SpringBoot+Vue3项目邮箱验证码注册详细教程
    引言该篇文章是以SpringBoot+Vue3技术栈搭建的项目的邮箱注册登录流程,注册用户使用邮箱校验,使用qq邮箱发送验证码,并且把验证码存入Redis以备校验使用的详细介绍干货部分1.在SpringBoot项目的application.yaml文件中配置qq邮箱spring:#mail邮箱mail:#SMTP......
  • WebGIS开发必学开源框架Openlayers(附赠视频教程+电子书)
    WebGIS开发之Openlayers当前,WebGIS开发领域的流行度不断攀升,导致市场上对该技能的需求与供应之间存在一定的紧张关系。在众多WebGIS开源框架中,如OpenLayers、Leaflet、MapBox、MapFish、GeoServer、GeoEXT和MapInfo等,企业通常期望应聘者能够掌握至少一种框架的开发能力,例如Op......
  • # Docker 私有仓库搭建教程
    Docker私有仓库是用于存储和管理Docker镜像的私有平台,对于企业来说,搭建私有仓库可以提高镜像的安全性和访问速度。本文将介绍几种搭建Docker私有仓库的方法。一、搭建Docker私有仓库的几种方式1.使用Docker官方Registry镜像Docker官方提供了一个用于构建私有镜......
  • RM1135、RM1135T量产修复成功,RTS5735DL量产工具操作教程,RTS5765DL、RTS5772DL开卡大致
    自己的固态坏了,本来打算找数据恢复公司恢复数据的,问了一下,大约需要上千块钱,算了,自己的数据还没这么值钱,于是就直接开卡了。这里把我自己研究的开卡方法分享给大家,注意开卡后硬盘数据会完全被擦除,不能恢复,所以有重要数据的话要提前备份!不好好看提示出了问题不要找我。开卡前必须准备......