import{_ as i,c as o,a as d,b as n,d as s,e,w as l,r,o as p}from"./app-CDdN0tOz.js";const m={};function c(u,t){const a=r("RouteLink");return p(),o("div",null,[t[18]||(t[18]=d('<h1 id="protobuf弹幕" tabindex="-1"><a class="header-anchor" href="#protobuf弹幕"><span>protobuf弹幕</span></a></h1><p>2020年5月23日，哔哩哔哩网页端及移动端启用了新的默认弹幕 API，网页端弹幕显示的上限变为原弹幕池上限的两倍。</p><p>新的 API 是以 6min 为一个单位加载，即每次加载 6min 内的弹幕</p><h2 id="获取实时弹幕" tabindex="-1"><a class="header-anchor" href="#获取实时弹幕"><span>获取实时弹幕</span></a></h2><blockquote><p>https://api.bilibili.com/x/v2/dm/web/seg.so （web端）</p><p>https://api.bilibili.com/x/v2/dm/wbi/web/seg.so （web 端新接口，需要 wbi 认证）</p><p>https://api.bilibili.com/x/v2/dm/list/seg.so （APP端）</p><p>https://i0.hdslb.com/bfs/dm/{data}.bin （BAS/代码弹幕专包）</p></blockquote><p><em>请求方式：GET</em><em>认证方式：半匿名（部分视频在无 Cookie: SESSDATA 时只返回部分弹幕）</em></p><p>此接口与漫画弹幕相同</p>',7)),n("p",null,[t[1]||(t[1]=s("只能返回普通弹幕（")),t[2]||(t[2]=n("code",null,"pool=1",-1)),t[3]||(t[3]=s()),t[4]||(t[4]=n("code",null,"mode=1-7",-1)),t[5]||(t[5]=s("）和代码弹幕（")),t[6]||(t[6]=n("code",null,"pool=2",-1)),t[7]||(t[7]=s()),t[8]||(t[8]=n("code",null,"mode=8",-1)),t[9]||(t[9]=s("），BAS弹幕（")),t[10]||(t[10]=n("code",null,"pool=2",-1)),t[11]||(t[11]=s()),t[12]||(t[12]=n("code",null,"mode=9",-1)),t[13]||(t[13]=s("）请从")),e(a,{to:"/docs/danmaku/danmaku_view_proto.html"},{default:l(()=>t[0]||(t[0]=[s("弹幕元数据")])),_:1,__:[0]}),t[14]||(t[14]=s("中获取"))]),n("p",null,[t[16]||(t[16]=s("互动弹幕（UP 主头像弹幕、关联视频、内嵌关注按钮）也不存在这个接口，请从")),e(a,{to:"/docs/danmaku/danmaku_view_proto.html"},{default:l(()=>t[15]||(t[15]=[s("弹幕元数据")])),_:1,__:[15]}),t[17]||(t[17]=s("中获取"))]),t[19]||(t[19]=d(`<p><strong>注：仅获取 6min 的整数倍时间内的弹幕，6min 内最多弹幕数为 6000 条（如第一包中弹幕<code>progress</code>值域为0-360000）</strong></p><p><strong>url参数：</strong></p><table><thead><tr><th>参数名</th><th>类型</th><th>内容</th><th>必要性</th><th>备注</th></tr></thead><tbody><tr><td>type</td><td>num</td><td>弹幕类</td><td>必要</td><td>1：视频弹幕<br>2：漫画弹幕</td></tr><tr><td>oid</td><td>num</td><td>视频 cid</td><td>必要</td><td></td></tr><tr><td>pid</td><td>num</td><td>稿件 avid</td><td>非必要</td><td></td></tr><tr><td>segment_index</td><td>num</td><td>分包</td><td>必要</td><td>6min 一包</td></tr><tr><td>pull_mode</td><td>num</td><td>（？）</td><td>非必要</td><td></td></tr><tr><td>ps</td><td>num</td><td>（？）</td><td>非必要</td><td></td></tr><tr><td>pe</td><td>num</td><td>（？）</td><td>非必要</td><td></td></tr></tbody></table><p><strong>proto回复：</strong></p><p>proto定义见：<a href="../../grpc_api/bilibili/community/service/dm/v1/dm.proto">bilibili.community.service.dm.v1.DmSegMobileReply</a></p><ul><li><p><a href="https://protogen.marcgravell.com/" target="_blank" rel="noopener noreferrer">protogen.marcgravell</a>: 在线编译 protogen 工具, 无需再安装本地编译器(生成文件需加后缀<code>_pb2.py</code>才可使用)</p></li><li><p><a href="https://pypi.org/project/protobuf/" target="_blank" rel="noopener noreferrer">protobuf pip</a>: 可一键安装的 Python 的 protogen 解析库</p></li></ul><p>消息<code>DmSegMobileReply</code>：</p><table><thead><tr><th>名称</th><th>类型</th><th>含义</th><th>备注</th></tr></thead><tbody><tr><td>elems</td><td>repeated DanmakuElem</td><td>弹幕条目</td><td></td></tr></tbody></table><p>消息<code>DanmakuElem</code>：</p><table><thead><tr><th>名称</th><th>类型</th><th>含义</th><th>备注</th></tr></thead><tbody><tr><td>id</td><td>int64</td><td>弹幕 dmid</td><td>唯一 可用于操作参数</td></tr><tr><td>progress</td><td>int32</td><td>视频内弹幕出现时间</td><td>毫秒</td></tr><tr><td>mode</td><td>int32</td><td>弹幕类型</td><td>1 2 3：普通弹幕<br>4：底部弹幕<br>5：顶部弹幕<br>6：逆向弹幕<br>7：高级弹幕<br>8：代码弹幕<br>9：BAS 弹幕（仅限于特殊弹幕专包）</td></tr><tr><td>fontsize</td><td>int32</td><td>弹幕字号</td><td>18：小<br>25：标准<br>36：大</td></tr><tr><td>color</td><td>uint32</td><td>弹幕颜色</td><td>十进制 RGB888 值</td></tr><tr><td>midHash</td><td>string</td><td>发送者 mid 的 HASH</td><td>用于屏蔽用户和查看用户发送的所有弹幕，也可反查用户id</td></tr><tr><td>content</td><td>string</td><td>弹幕内容</td><td>utf-8编码</td></tr><tr><td>ctime</td><td>int64</td><td>弹幕发送时间</td><td>时间戳</td></tr><tr><td>weight</td><td>int32</td><td>权重</td><td>用于智能屏蔽，根据弹幕语义及长度通过AI识别得出<br>范围：[0-10]<br>值越大权重越高</td></tr><tr><td>action</td><td>string</td><td>动作？</td><td></td></tr><tr><td>pool</td><td>int32</td><td>弹幕池</td><td>0：普通池<br>1：字幕池<br>2：特殊池（代码/BAS弹幕）</td></tr><tr><td>idStr</td><td>string</td><td>弹幕 dmid</td><td>字串形式<br>唯一 可用于操作参数</td></tr><tr><td>attr</td><td>int32</td><td>弹幕属性位</td><td>bit0：保护<br>bit1：直播<br>bit2：高赞</td></tr><tr><td>animation</td><td>string</td><td>动画？</td><td></td></tr></tbody></table><p><strong>示例：</strong></p><p>获取视频<code>av810872(cid=1176840)</code>（炮姐）的实时弹幕分包 1</p><p><strong>注：以下<a href="../grpc_api/bilibili/community/service/dm/v1/dm.proto">proto定义</a>需要编译，<code>bilibili.community.service.dm.v1.dm_pb2</code>并非通过 pypi 安装</strong></p><div class="language-python line-numbers-mode" data-highlighter="shiki" data-ext="python" style="background-color:#1E1E1E;color:#D4D4D4;"><pre class="shiki dark-plus vp-code"><code class="language-python"><span class="line"><span style="color:#C586C0;">import</span><span style="color:#D4D4D4;"> requests</span></span>
<span class="line"><span style="color:#C586C0;">import</span><span style="color:#D4D4D4;"> google.protobuf.text_format </span><span style="color:#C586C0;">as</span><span style="color:#D4D4D4;"> text_format</span></span>
<span class="line"><span style="color:#C586C0;">import</span><span style="color:#D4D4D4;"> bilibili.community.service.dm.v1.dm_pb2 </span><span style="color:#C586C0;">as</span><span style="color:#D4D4D4;"> Danmaku</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D4D4D4;">url = </span><span style="color:#CE9178;">&#39;https://api.bilibili.com/x/v2/dm/web/seg.so&#39;</span></span>
<span class="line"><span style="color:#D4D4D4;">params = {</span></span>
<span class="line"><span style="color:#CE9178;">    &#39;type&#39;</span><span style="color:#D4D4D4;">: </span><span style="color:#B5CEA8;">1</span><span style="color:#D4D4D4;">,         </span><span style="color:#6A9955;"># 弹幕类型</span></span>
<span class="line"><span style="color:#CE9178;">    &#39;oid&#39;</span><span style="color:#D4D4D4;">: </span><span style="color:#B5CEA8;">1176840</span><span style="color:#D4D4D4;">,    </span><span style="color:#6A9955;"># cid</span></span>
<span class="line"><span style="color:#CE9178;">    &#39;pid&#39;</span><span style="color:#D4D4D4;">: </span><span style="color:#B5CEA8;">810872</span><span style="color:#D4D4D4;">,     </span><span style="color:#6A9955;"># avid</span></span>
<span class="line"><span style="color:#CE9178;">    &#39;segment_index&#39;</span><span style="color:#D4D4D4;">: </span><span style="color:#B5CEA8;">1</span><span style="color:#6A9955;"> # 弹幕分段</span></span>
<span class="line"><span style="color:#D4D4D4;">}</span></span>
<span class="line"><span style="color:#D4D4D4;">resp = requests.get(url, params)</span></span>
<span class="line"><span style="color:#D4D4D4;">data = resp.content</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D4D4D4;">danmaku_seg = Danmaku.DmSegMobileReply()</span></span>
<span class="line"><span style="color:#D4D4D4;">danmaku_seg.ParseFromString(data)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#DCDCAA;">print</span><span style="color:#D4D4D4;">(text_format.MessageToString(danmaku_seg.elems[</span><span style="color:#B5CEA8;">0</span><span style="color:#D4D4D4;">], </span><span style="color:#9CDCFE;">as_utf8</span><span style="color:#D4D4D4;">=</span><span style="color:#569CD6;">True</span><span style="color:#D4D4D4;">))</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>输出：</p><div class="language- line-numbers-mode" data-highlighter="shiki" data-ext="" style="background-color:#1E1E1E;color:#D4D4D4;"><pre class="shiki dark-plus vp-code"><code class="language-"><span class="line"><span>id: 711923911</span></span>
<span class="line"><span>progress: 47880</span></span>
<span class="line"><span>mode: 1</span></span>
<span class="line"><span>fontsize: 18</span></span>
<span class="line"><span>color: 10092288</span></span>
<span class="line"><span>midHash: &quot;59417e95&quot;</span></span>
<span class="line"><span>content: &quot;世界第一电击公主殿下,遇到你是我一生最美好的风景!吾炮赛高,永生不离!唯我超电磁炮永世长存! &quot;</span></span>
<span class="line"><span>ctime: 1418799826</span></span>
<span class="line"><span>weight: 6</span></span>
<span class="line"><span>idStr: &quot;711923911&quot;</span></span>
<span class="line"><span>attr: 1</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div>`,16))])}const v=i(m,[["render",c]]),D=JSON.parse('{"path":"/docs/danmaku/danmaku_proto.html","title":"protobuf弹幕","lang":"zh-CN","frontmatter":{},"git":{"updatedTime":1696153717000,"contributors":[{"name":"SocialSisterYi","username":"SocialSisterYi","email":"1440239038@qq.com","commits":3,"url":"https://github.com/SocialSisterYi"},{"name":"gwy15","username":"gwy15","email":"gwy15thu@gmail.com","commits":1,"url":"https://github.com/gwy15"},{"name":"LaMerChiang","username":"LaMerChiang","email":"catlair@qq.com","commits":1,"url":"https://github.com/LaMerChiang"}],"changelog":[{"hash":"4dac6cc2d0013c06c5cdf397d9dfc72a1c3531c4","time":1696153717000,"email":"catlair@qq.com","author":"LaMerChiang","message":"标注部分 wbi 链接 (#827)"},{"hash":"c2d9291c0026350e07c01e59fc8a3ee0e0cd42f1","time":1693028780000,"email":"gwy15thu@gmail.com","author":"gwy15","message":"Update danmaku_proto.md，增加认证说明 (#792)"},{"hash":"87d56b66e7ce375029bccf9efda5314964b0528d","time":1684287732000,"email":"1440239038@qq.com","author":"SocialSisterYi","message":"update 【danmaku thumbup list】 APIDoc"},{"hash":"f6760f4be38d5b592d396b211e48c666286524de","time":1677124138000,"email":"1440239038@qq.com","author":"SocialSisterYi","message":"🔨remove header index"},{"hash":"7d89ece2ac46425810647c4ac92acf5f3721cb68","time":1676998806000,"email":"1440239038@qq.com","author":"SocialSisterYi","message":"🚀调整项目结构"}]},"filePathRelative":"docs/danmaku/danmaku_proto.md"}');export{v as comp,D as data};
