栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

b站 实时弹幕和历史弹幕 Protobuf 格式解析

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

b站 实时弹幕和历史弹幕 Protobuf 格式解析

参考:

https://zhuanlan.zhihu.com/p/392931611https://gitee.com/nbody1996/bilibili-API-collect/blob/master/danmaku/danmaku_proto.mdBilibili 历史弹幕:https://www.cnblogs.com/mollnn/p/14964905.html

b站弹幕传输的格式由原来的 xml 改为了 protobuf,这个格式为二进制编码传输,其传输销量远高于原来的 xml,因此在移动端可以减小网络的压力具有一定的优势。但带来的一个问题就是,这个格式的弹幕解析起来变得十分困难,通常从 api 获得的数据直接看是一通乱码,需要特定的方式才能看到真正的内容,让人比较头疼。

B站使用 protobuf 协议之前的弹幕接口

https://comment.bilibili.com/4934490.xml
https://api.bilibili.com/x/v1/dm/list.so?oid=4934490

1、什么是 Protobuf

Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.

上面这段话来自谷歌 Protobuf 官网的介绍,简单来讲就是一种传输的协议,比 xml 更小、更快、更简单,更多信息可以见:https://developers.google.com/protocol-buffers/

2、如何解析 Protobuf 的弹幕

2.1 下载 Protoc 编译器

Protoc 是用于将 .proto 文件编译成各种编程语言(如 Python、Golang 等)的编译器,是进行 Protobuf 解析的必要条件,可在下面的链接中下载:https://github.com/protocolbuffers/protobuf

 下载完成后解压出来是 exe 文件,不需要安装,但是需要手动添加到 Path 中。

 通过在终端中运行如下代码来确定是否安装成功:protoc --version

2.2 下载 Protobuf-Python 以便在 Python 中解析 Protobuf

下载地址:https://github.com/protocolbuffers/protobuf

下载完成后解压,然后进入 python 进入目录,

 执行以下命令行代码:

python setup.py clean
python setup.py build
python setup.py install
python setup.py test

2.3 弹幕的 proto 定义并编译

弹幕格式,protobuf 结构体:

dm.proto

syntax = "proto3";

package dm;

message DmSegMobileReply{
  repeated DanmakuElem elems = 1;
}
message DanmakuElem{
  int64 id = 1;
  int32 progress = 2;
  int32 mode = 3;
  int32 fontsize = 4;
  uint32 color = 5;
  string midHash = 6;
  string content = 7;
  int64 ctime = 8;
  int32 weight = 9;
  string action = 10;
  int32 pool = 11;
  string idStr = 12;
}
名称含义类型备注
id弹幕dmIDint64唯一 可用于操作参数
progress视频内弹幕出现时间int32毫秒
mode弹幕类型int321 2 3:普通弹幕
4:底部弹幕
5:顶部弹幕
6:逆向弹幕
7:高级弹幕
8:代码弹幕
9:BAS弹幕
fontsize弹幕字号int3218:小
25:标准
36:大
color弹幕颜色uint32十进制RGB888值
midHash发送者UID的HASHstring用于屏蔽用户和查看用户发送的所有弹幕 也可反查用户ID
content弹幕内容stringutf-8编码
ctime弹幕发送时间int64时间戳
weight权重int32用于智能屏蔽级别
action动作string未知
pool弹幕池int320:普通池
1:字幕池
2:特殊池(代码/BAS弹幕)
idStr弹幕dmID的字符串类型string唯一 可用于操作参数

2.4 解析 seg.so 格式的弹幕数据

示例视频:https://www.bilibili.com/video/av98919207

解析之前需要先安装 python 的 probuf 包: pip install protobuf

编译 proto 结构文件,

protoc --python_out=. dm.proto

执行完成后会生成 dm_pb2.py,代码中引入这个 python 文件,

 dm_pj.py 代码如下:

注意:

实时弹幕 不需要 cookie,直接请求即可得到 seg.so 历史弹幕 需要 cookie 才能得到 seg.so 

# -*- coding: utf-8 -*-
# @Author  : 
# @Date    :
# @File    : dm_pj.py
# @description : XXX


import json
import requests
from dm_pb2 import DmSegMobileReply
from google.protobuf.json_format import MessageToJson, Parse


b_web_cookie = 'SESSDATA=fd25e2e6%2C1660373048%2C287c9%2A21;'


def get_date_list():
    url = "https://api.bilibili.com/x/v2/dm/history/index?type=1&oid=168855206&month=2022-02"
    headers = {
        'cookie': b_web_cookie
    }
    response = requests.get(url, headers=headers)
    print(json.dumps(response.json(), ensure_ascii=False, indent=4))


def dm_real_time():
    url_real_time = 'https://api.bilibili.com/x/v2/dm/web/seg.so?type=1&oid=168855206&pid=98919207&segment_index=1'
    resp = requests.get(url_real_time)

    DM = DmSegMobileReply()
    DM.ParseFromString(resp.content)
    data_dict = json.loads(MessageToJson(DM))
    # print(data_dict)
    list(map(lambda x=None: print(x['content']), data_dict.get('elems', [])))


def dm_history():
    url_history = 'https://api.bilibili.com/x/v2/dm/web/history/seg.so?type=1&oid=168855206&date=2022-02-23'
    headers = {
        'cookie': b_web_cookie
    }
    resp = requests.get(url_history, headers=headers)
    DM = DmSegMobileReply()
    DM.ParseFromString(resp.content)
    data_dict = json.loads(MessageToJson(DM))
    # print(data_dict)
    list(map(lambda x=None: print(x['content']), data_dict.get('elems', [])))


if __name__ == '__main__':
    # dm_real_time()
    get_date_list()
    # dm_history()
    pass

 执行结果截图:

弹幕对比:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/753340.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号