b站 实时弹幕和历史弹幕 Protobuf 格式解析
參考:
- https://zhuanlan.zhihu.com/p/392931611
- https://gitee.com/nbody1996/bilibili-API-collect/blob/master/danmaku/danmaku_proto.md
- Bilibili 歷史彈幕:https://www.cnblogs.com/mollnn/p/14964905.html
b站彈幕傳輸?shù)母袷接稍瓉?lái)的 xml 改為了 protobuf,這個(gè)格式為二進(jìn)制編碼傳輸,其傳輸銷(xiāo)量遠(yuǎn)高于原來(lái)的 xml,因此在移動(dòng)端可以減小網(wǎng)絡(luò)的壓力具有一定的優(yōu)勢(shì)。但帶來(lái)的一個(gè)問(wèn)題就是,這個(gè)格式的彈幕解析起來(lái)變得十分困難,通常從 api 獲得的數(shù)據(jù)直接看是一通亂碼,需要特定的方式才能看到真正的內(nèi)容,讓人比較頭疼。
B站沒(méi)有使用?protobuf 協(xié)議前的彈幕接口
- :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.
上面這段話(huà)來(lái)自谷歌 Protobuf 官網(wǎng)的介紹,簡(jiǎn)單來(lái)講就是一種傳輸?shù)膮f(xié)議,比 xml 更小、更快、更簡(jiǎn)單,更多信息可以見(jiàn):https://developers.google.com/protocol-buffers/
2、如何解析 Protobuf 的彈幕
2.1 下載 Protoc 編譯器
Protoc 是用于將 .proto 文件編譯成各種編程語(yǔ)言(如 Python、Golang 等)的編譯器,是進(jìn)行 Protobuf 解析的必要條件,可在下面的鏈接中下載:https://github.com/protocolbuffers/protobuf
?下載完成后解壓出來(lái)是 exe 文件,不需要安裝,但是需要手動(dòng)添加到 Path 中。
?通過(guò)在終端中運(yùn)行如下代碼來(lái)確定是否安裝成功:protoc --version
2.2 下載 Protobuf-Python 以便在 Python 中解析 Protobuf
下載地址:https://github.com/protocolbuffers/protobuf
下載完成后解壓,然后進(jìn)入?python 進(jìn)入目錄,
?執(zhí)行以下命令行代碼:
python setup.py clean python setup.py build python setup.py install python setup.py test2.3 彈幕的 proto 定義并編譯
彈幕格式,protobuf 結(jié)構(gòu)體:
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 | 彈幕dmID | int64 | 唯一 可用于操作參數(shù) |
| progress | 視頻內(nèi)彈幕出現(xiàn)時(shí)間 | int32 | 毫秒 |
| mode | 彈幕類(lèi)型 | int32 | 1 2 3:普通彈幕 4:底部彈幕 5:頂部彈幕 6:逆向彈幕 7:高級(jí)彈幕 8:代碼彈幕 9:BAS彈幕 |
| fontsize | 彈幕字號(hào) | int32 | 18:小 25:標(biāo)準(zhǔn) 36:大 |
| color | 彈幕顏色 | uint32 | 十進(jìn)制RGB888值 |
| midHash | 發(fā)送者UID的HASH | string | 用于屏蔽用戶(hù)和查看用戶(hù)發(fā)送的所有彈幕 也可反查用戶(hù)ID |
| content | 彈幕內(nèi)容 | string | utf-8編碼 |
| ctime | 彈幕發(fā)送時(shí)間 | int64 | 時(shí)間戳 |
| weight | 權(quán)重 | int32 | 用于智能屏蔽級(jí)別 |
| action | 動(dòng)作 | string | 未知 |
| pool | 彈幕池 | int32 | 0:普通池 1:字幕池 2:特殊池(代碼/BAS彈幕) |
| idStr | 彈幕dmID的字符串類(lèi)型 | string | 唯一 可用于操作參數(shù) |
2.4 解析 seg.so 格式的彈幕數(shù)據(jù)
示例視頻:https://www.bilibili.com/video/av98919207
解析之前需要先安裝 python 的 probuf 包: pip install?protobuf
編譯 proto 結(jié)構(gòu)文件,
protoc --python_out=. dm.proto執(zhí)行完成后會(huì)生成 dm_pb2.py,代碼中引入這個(gè) python 文件,
?dm_pj.py 代碼如下:
注意:
- 實(shí)時(shí)彈幕 不需要 cookie,直接請(qǐng)求即可得到 seg.so?
- 歷史彈幕 需要 cookie 才能得到?seg.so?
?執(zhí)行結(jié)果截圖:
彈幕對(duì)比:
總結(jié)
以上是生活随笔為你收集整理的b站 实时弹幕和历史弹幕 Protobuf 格式解析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Boost学习之正则表达式--regex
- 下一篇: 《Wireshark协议分析从入门到精通