吴彦祖 发表于 2024-4-19 09:14:10

下载 bilibili 字幕的脚本, 分享一下

本帖最后由 牙签执法官 于 2024-4-19 09:17 编辑

想下载一个视频教程,发现字幕没有下载. 网上也没有工具,代码如下:
"""下载哔哩哔哩 字幕
"""
import math
import os
import time

import requests
import json


def convert_json_to_srt(json_files_path):
    """
    json 格式的字幕转为 srt 格式
    代码来源 https://www.jianshu.com/p/66450e9554f8
    """
    json_files = os.listdir(json_files_path)
    srt_files_path = os.path.join(json_files_path, 'srt')# 更改后缀后字幕文件的路径
    isExists = os.path.exists(srt_files_path)
    if not isExists:
      os.mkdir(srt_files_path)

    for json_file in json_files:
      file_name = json_file.replace(json_file[-5:], '.srt')# 改变转换后字幕的后缀
      file = ''# 这个变量用来保存数据
      i = 1
      # 将此处文件位置进行修改,加上utf-8是为了避免处理中文时报错
      with open(os.path.join(json_files_path, json_file), encoding='utf-8') as f:
            datas = json.load(f)# 加载文件数据
            f.close()

      for data in datas['body']:
            start = data['from']# 获取开始时间
            stop = data['to']# 获取结束时间
            content = data['content']# 获取字幕内容
            file += '{}\n'.format(i)# 加入序号
            hour = math.floor(start) // 3600
            minute = (math.floor(start) - hour * 3600) // 60
            sec = math.floor(start) - hour * 3600 - minute * 60
            minisec = int(math.modf(start) * 100)# 处理开始时间
            file += str(hour).zfill(2) + ':' + str(minute).zfill(2) + ':' + str(sec).zfill(2) + ',' + str(
                minisec).zfill(2)# 将数字填充0并按照格式写入
            file += ' --> '
            hour = math.floor(stop) // 3600
            minute = (math.floor(stop) - hour * 3600) // 60
            sec = math.floor(stop) - hour * 3600 - minute * 60
            minisec = abs(int(math.modf(stop) * 100 - 1))# 此处减1是为了防止两个字幕同时出现
            file += str(hour).zfill(2) + ':' + str(minute).zfill(2) + ':' + str(sec).zfill(2) + ',' + str(
                minisec).zfill(2)
            file += '\n' + content + '\n\n'# 加入字幕文字
            i += 1
      with open(os.path.join(srt_files_path, file_name), 'w', encoding='utf-8') as f:
            f.write(file)# 将数据写入文件


def download_subtitle_json(bvid: str):
    """
    下载字幕
    """
    sub_dir = f'./{bvid}'
    if not os.path.isdir(sub_dir):
      os.mkdir('./{bvid}')
    headers = {
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0',
      'Accept': 'application/json, text/plain, */*',
      'Accept-Language': 'en-US,en;q=0.5',
      # 'Accept-Encoding': 'gzip, deflate, br',
      'Referer': 'https://www.bilibili.com/video/{bvid}/?p=1',
      'Origin': 'https://www.bilibili.com',
      'Connection': 'keep-alive',
      # TODO 改为 自己的cookie , 通过浏览器的 network(网络) 复制
      'Cookie': "",
      'Sec-Fetch-Dest': 'empty',
      'Sec-Fetch-Mode': 'cors',
      'Sec-Fetch-Site': 'same-site',
    }
    resp = requests.get(f'https://www.bilibili.com/video/{bvid}/', headers=headers)
    text = resp.text
    aid = text
    aid = aid[:aid.find(',')]
    cid_back = requests.get("http://api.bilibili.com/x/player/pagelist?bvid={}".format(bvid), headers=headers)
    if cid_back.status_code != 200:
      print('获取 playlist 失败')

    cid_json = json.loads(cid_back.content)
    for item in cid_json['data']:
      cid = item['cid']
      title = item['part'] + '.json'

      params = {
            'aid': aid,
            'cid': cid,
            'isGaiaAvoided': 'false',
            'web_location': '1315873',
            'w_rid': '364cdf378b75ef6a0cee77484ce29dbb',
            'wts': int(time.time()),
      }

      wbi_resp = requests.get('https://api.bilibili.com/x/player/wbi/v2', params=params, headers=headers)
      if wbi_resp.status_code != 200:
            print('获取 字幕链接 失败')
      subtitle_links = wbi_resp.json()['data']["subtitle"]['subtitles']
      if subtitle_links:
            # 默认下载第一个字幕
            subtitle_url = "https:" + subtitle_links['subtitle_url']
            subtitle_resp = requests.get(subtitle_url, headers=headers)
            open(os.path.join(sub_dir, title), 'w', encoding='utf-8').write(subtitle_resp.text)


if __name__ == '__main__':
    # todo 改成需要下载的 bvid, https://www.bilibili.com/video/<bvid>
    BVID = 'BV1s8411v7nE'
    download_subtitle_json(BVID)
    # convert_json_to_srt(f'./{BVID}')


吴彦祖 发表于 2024-4-19 09:17:59

代码

SanS三石 发表于 2024-6-27 09:58:47

我觉得,不改变也很好。
页: [1]
查看完整版本: 下载 bilibili 字幕的脚本, 分享一下