叉叉白
首页
朋友们
留言板
关于我
GitHub (opens new window)

MonoLogueChi

菜鸡程序员
首页
朋友们
留言板
关于我
GitHub (opens new window)
  • 自己组建一个多功能导播台
  • 自己开发一个vuepress插件
  • 用Scoop安装一些实用工具
  • 博客现在的一些情况
  • Docker安装Caddy使用自定义插件
  • FMP4、HLS和DASH视频生成
  • 使用 GitHub Actions 将博客部署到 GitHub Page
  • 使用 GitHub Actions 将博客部署到又拍云
  • C#解析B站弹幕
    • 了解 protobuf
    • protobuf 定义
    • 实例
      • 创建项目
      • 获取数据
      • 解析数据
      • 序列化数据
      • 打印数据
  • 使用NDI双机位直播
  • 2022
MonologueChi
2022-06-05
目录

C#解析B站弹幕

两年前写过写一篇文章,BiliBili 弹幕解析,但上次是解析 xml 弹幕,最近打算研究一下 v2 弹幕接口,使用的是 protobuf。

# 了解 protobuf

在本文正式开始之前要先了解一下 protobuf,这个可以自行搜索,本文不在赘述。

# protobuf 定义

可以直接使用其他项目定义的,以下两个项目的定义文件均可直接使用,选择其一下载备用

  • bilibili-API-collect (opens new window)
  • Bili.Uwp (opens new window)

# 实例

# 创建项目

创建一个控制台项目作为演示使用,并使用 nuget 安装相关包

  • Google.Protobuf (opens new window)
  • Grpc.Net.Client (opens new window)
  • Grpc.Tools (opens new window)
  • Flurl (opens new window)
  • Flurl.Http (opens new window)
  • Newtonsoft.Json (opens new window)

将前面下载好的 protobuf 定义文件放入项目目录下,我创建的示例项目路径为 Models/Proto/dm.proto

修改项目文件,添加 protobuf 定义文件引用

<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFramework>net6.0</TargetFramework>
		<ImplicitUsings>enable</ImplicitUsings>
		<Nullable>enable</Nullable>
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="Flurl" Version="3.0.6" />
		<PackageReference Include="Flurl.Http" Version="3.2.4" />
		<PackageReference Include="Google.Protobuf" Version="3.21.1" />
		<PackageReference Include="Grpc.Net.Client" Version="2.46.0" />
		<PackageReference Include="Grpc.Tools" Version="2.46.3">
			<PrivateAssets>all</PrivateAssets>
			<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
		</PackageReference>
	</ItemGroup>

+	<ItemGroup>
+		<Protobuf Include="Models\Proto\dm.proto" />
+	</ItemGroup>

</Project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

# 获取数据

使用 v2 弹幕接口 https://api.bilibili.com/x/v2/dm/list/seg.so 获取数据

using Flurl;
using Flurl.Http;

var a = await "https://api.bilibili.com/x/v2/dm/list/seg.so".SetQueryParams(new
{
    type = 1,
    oid = 1176840,
    segment_index = 1
}).GetBytesAsync();
1
2
3
4
5
6
7
8
9

# 解析数据

using Bilibili.Community.Service.Dm.V1;
using Flurl;
using Flurl.Http;

var a = await "https://api.bilibili.com/x/v2/dm/list/seg.so".SetQueryParams(new
{
    type = 1,
    oid = 1176840,
    segment_index = 1
}).GetBytesAsync();

var b = DmSegMobileReply.Parser.ParseFrom(a);
1
2
3
4
5
6
7
8
9
10
11
12

# 序列化数据

using Bilibili.Community.Service.Dm.V1;
using Flurl;
using Flurl.Http;
using Newtonsoft.Json;

var a = await "https://api.bilibili.com/x/v2/dm/list/seg.so".SetQueryParams(new
{
    type = 1,
    oid = 1176840,
    segment_index = 1
}).GetBytesAsync();

var b = DmSegMobileReply.Parser.ParseFrom(a);

var c = JsonConvert.SerializeObject(b);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 打印数据

using Bilibili.Community.Service.Dm.V1;
using Flurl;
using Flurl.Http;
using Newtonsoft.Json;

var a = await "https://api.bilibili.com/x/v2/dm/list/seg.so".SetQueryParams(new
{
    type = 1,
    oid = 1176840,
    segment_index = 1
}).GetBytesAsync();

var b = DmSegMobileReply.Parser.ParseFrom(a);

var c = JsonConvert.SerializeObject(b);

Console.WriteLine(c);

Console.ReadKey();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

至此,本文完结。

查看MD文件 (opens new window)
#C##dotnet
上次更新: 7/6/2022, 11:32:52 AM
使用 GitHub Actions 将博客部署到又拍云
使用NDI双机位直播

← 使用 GitHub Actions 将博客部署到又拍云 使用NDI双机位直播→

最近更新
01
使用NDI双机位直播
07-04
02
使用 GitHub Actions 将博客部署到又拍云
03-09
03
使用 GitHub Actions 将博客部署到 GitHub Page
03-09
更多文章>
Theme by Vdoing | Copyright © 2016-2022 MonoLogueChi | CC BY-NC-SA 4.0
本站由 提供 CDN 加速 / 云存储服务
蒙ICP备17004911号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式