吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 882|回复: 31
收起左侧

[Python 原创] 写了一个下载 bilibili 字幕的脚本, 分享一下

  [复制链接]
master9 发表于 2024-4-13 19:06
想下载一个视频教程,发现字幕没有下载. 网上也没有工具,就自己写了一个


[Python] 纯文本查看 复制代码
"""下载哔哩哔哩 字幕
"""
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)[0] * 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)[0] * 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[text.find('"aid"') + 6:]
    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[0]['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}')

免费评分

参与人数 6威望 +1 吾爱币 +10 热心值 +5 收起 理由
wenguanbao + 1 谢谢@Thanks!
echoaku + 1 + 1 谢谢@Thanks!
苏紫方璇 + 1 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
qq9953 + 1 谢谢@Thanks!
YukikazeR + 1 谢谢@Thanks!
huchen + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

bingtuzibb 发表于 2024-4-14 03:13
小白求成品。。
huchen 发表于 2024-4-13 23:33
aliya0416 发表于 2024-4-14 02:05
windwithme6 发表于 2024-4-14 02:33
来学习以一下
yoyomi 发表于 2024-4-14 07:35
很好的思路,学习了。
sunflash 发表于 2024-4-14 08:27
非常有用,感谢分享
rjyq168 发表于 2024-4-14 08:31
那个闪豆是不是不能用了?
wkdxz 发表于 2024-4-14 08:53
不错的软件,对我帮助很大
不苦小和尚 发表于 2024-4-14 09:01
试试看看,自己手动敲一遍
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-30 06:04

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表