日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

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

發(fā)布時(shí)間:2024/7/23 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 b站 实时弹幕和历史弹幕 Protobuf 格式解析 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

參考:

  • 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ù)母袷接稍瓉淼?xml 改為了 protobuf,這個(gè)格式為二進(jìn)制編碼傳輸,其傳輸銷量遠(yuǎn)高于原來的 xml,因此在移動(dòng)端可以減小網(wǎng)絡(luò)的壓力具有一定的優(yōu)勢。但帶來的一個(gè)問題就是,這個(gè)格式的彈幕解析起來變得十分困難,通常從 api 獲得的數(shù)據(jù)直接看是一通亂碼,需要特定的方式才能看到真正的內(nèi)容,讓人比較頭疼。

B站沒有使用?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.

上面這段話來自谷歌 Protobuf 官網(wǎng)的介紹,簡單來講就是一種傳輸?shù)膮f(xié)議,比 xml 更小、更快、更簡單,更多信息可以見:https://developers.google.com/protocol-buffers/

2、如何解析 Protobuf 的彈幕

2.1 下載 Protoc 編譯器

Protoc 是用于將 .proto 文件編譯成各種編程語言(如 Python、Golang 等)的編譯器,是進(jìn)行 Protobuf 解析的必要條件,可在下面的鏈接中下載:https://github.com/protocolbuffers/protobuf

?下載完成后解壓出來是 exe 文件,不需要安裝,但是需要手動(dòng)添加到 Path 中。

?通過在終端中運(yùn)行如下代碼來確定是否安裝成功: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 test

2.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彈幕dmIDint64唯一 可用于操作參數(shù)
progress視頻內(nèi)彈幕出現(xiàn)時(shí)間int32毫秒
mode彈幕類型int321 2 3:普通彈幕
4:底部彈幕
5:頂部彈幕
6:逆向彈幕
7:高級(jí)彈幕
8:代碼彈幕
9:BAS彈幕
fontsize彈幕字號(hào)int3218:小
25:標(biāo)準(zhǔn)
36:大
color彈幕顏色uint32十進(jìn)制RGB888值
midHash發(fā)送者UID的HASHstring用于屏蔽用戶和查看用戶發(fā)送的所有彈幕 也可反查用戶ID
content彈幕內(nèi)容stringutf-8編碼
ctime彈幕發(fā)送時(shí)間int64時(shí)間戳
weight權(quán)重int32用于智能屏蔽級(jí)別
action動(dòng)作string未知
pool彈幕池int320:普通池
1:字幕池
2:特殊池(代碼/BAS彈幕)
idStr彈幕dmID的字符串類型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?
# -*- coding: utf-8 -*- # @Author : # @Date : # @File : dm_pj.py # @description : XXXimport json import requests from dm_pb2 import DmSegMobileReply from google.protobuf.json_format import MessageToJson, Parseb_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

?執(zhí)行結(jié)果截圖:

彈幕對(duì)比:

總結(jié)

以上是生活随笔為你收集整理的b站 实时弹幕和历史弹幕 Protobuf 格式解析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。