forked from nilaoda/BBDown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.cs
471 lines (428 loc) · 22.6 KB
/
Parser.cs
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
using System.Text;
using System.Text.RegularExpressions;
using System.Text.Json;
using static BBDown.Core.Logger;
using static BBDown.Core.Util.HTTPUtil;
using static BBDown.Core.Entity.Entity;
using System.Security.Cryptography;
namespace BBDown.Core
{
public partial class Parser
{
public static string WbiSign(string api)
{
return $"{api}&w_rid=" + string.Concat(MD5.HashData(Encoding.UTF8.GetBytes(api + Config.WBI)).Select(i => i.ToString("x2")).ToArray());
}
private static async Task<string> GetPlayJsonAsync(string encoding, string aidOri, string aid, string cid, string epId, bool tvApi, bool intl, bool appApi, string qn = "0")
{
LogDebug("aid={0},cid={1},epId={2},tvApi={3},IntlApi={4},appApi={5},qn={6}", aid, cid, epId, tvApi, intl, appApi, qn);
if (intl) return await GetPlayJsonAsync(aid, cid, epId, qn);
bool cheese = aidOri.StartsWith("cheese:");
bool bangumi = cheese || aidOri.StartsWith("ep:");
LogDebug("bangumi={0},cheese={1}", bangumi, cheese);
if (appApi) return await AppHelper.DoReqAsync(aid, cid, epId, qn, bangumi, encoding, Config.TOKEN);
string prefix = tvApi ? bangumi ? "api.snm0516.aisee.tv/pgc/player/api/playurltv" : "api.snm0516.aisee.tv/x/tv/ugc/playurl"
: bangumi ? $"{Config.HOST}/pgc/player/web/playurl" : "api.bilibili.com/x/player/wbi/playurl";
prefix = $"https://{prefix}?";
string api;
if (tvApi && bangumi)
{
api = (Config.TOKEN != "" ? $"access_key={Config.TOKEN}&" : "") +
$"aid={aid}&appkey=4409e2ce8ffd12b8&build=102801" +
$"&cid={cid}&device=android&ep_id={epId}&expire=0" +
$"&fnval=4048&fnver=0&fourk=1" +
$"&mid=0&mobi_app=android_tv_yst" +
$"&module=bangumi&npcybs=0&otype=json&platform=android" +
$"&qn={qn}&ts={GetTimeStamp(true)}";
api = prefix + api + $"&sign={GetSign(api, false)}";
}
else
{
// 尝试提高可读性
StringBuilder apiBuilder = new();
apiBuilder.Append($"avid={aid}&cid={cid}&fnval=4048&fnver=0&fourk=1");
if (Config.AREA != "") apiBuilder.Append($"&access_key={Config.TOKEN}&area={Config.AREA}");
if (tvApi)
{
apiBuilder.Append("&device=android&platform=android&mobi_app=android_tv_yst&npcybs=0&force_host=2&build=102801");
if(Config.TOKEN != "") apiBuilder.Append($"&access_key={Config.TOKEN}");
}
apiBuilder.Append($"&otype=json&qn={qn}");
if (bangumi) apiBuilder.Append($"&module=bangumi&ep_id={epId}&session=");
else if (!tvApi)
{
if (Config.COOKIE == "") apiBuilder.Append("&try_look=1");
apiBuilder.Append($"&wts={GetTimeStamp(true)}");
}
api = prefix + ((bangumi || tvApi) ? apiBuilder.ToString() : WbiSign(apiBuilder.ToString()));
}
//课程接口
if (cheese) api = api.Replace("/pgc/", "/pugv/");
//Console.WriteLine(api);
string webJson = await GetWebSourceAsync(api);
//以下情况从网页源代码尝试解析
if (webJson.Contains("\"大会员专享限制\""))
{
Log("此视频需要大会员,您大概率需要登录一个有大会员的账号才可以下载,尝试从网页源码解析");
string webUrl = "https://www.bilibili.com/bangumi/play/ep" + epId;
string webSource = await GetWebSourceAsync(webUrl);
webJson = PlayerJsonRegex().Match(webSource).Groups[1].Value;
}
return webJson;
}
private static async Task<string> GetPlayJsonAsync(string aid, string cid, string epId, string qn, string code = "0")
{
bool isBiliPlus = Config.HOST == "api.bilibili.com";
string api = $"https://{(isBiliPlus ? Config.HOST : "api.biliintl.com")}/intl/gateway/v2/ogv/playurl?";
StringBuilder paramBuilder = new();
if (Config.TOKEN != "") paramBuilder.Append($"access_key={Config.TOKEN}&");
paramBuilder.Append($"aid={aid}");
if (isBiliPlus) paramBuilder.Append($"&appkey=7d089525d3611b1c&area={(Config.AREA == "" ? "th" : Config.AREA)}");
paramBuilder.Append($"&cid={cid}&ep_id={epId}&platform=android&prefer_code_type={code}&qn={qn}");
if (isBiliPlus) paramBuilder.Append($"&ts={GetTimeStamp(true)}");
string param = paramBuilder.ToString();
api += (isBiliPlus ? $"{param}&sign={GetSign(param, true)}" : param);
string webJson = await GetWebSourceAsync(api);
return webJson;
}
public static async Task<(string, List<Video>, List<Audio>, List<string>, List<string>, List<Audio>, List<AudioMaterialInfo>, List<ViewPoint>)> ExtractTracksAsync(string aidOri, string aid, string cid, string epId, bool tvApi, bool intlApi, bool appApi, string encoding, string qn = "0")
{
List<Video> videoTracks = new();
List<Audio> audioTracks = new();
List<string> clips = new();
List<string> dfns = new();
List<Audio> backgroundAudioTracks = new();
List<AudioMaterialInfo> roleAudioList = new();
List<ViewPoint> points = new();
var intlCode = "0";
//调用解析
string webJsonStr = await GetPlayJsonAsync(encoding, aidOri, aid, cid, epId, tvApi, intlApi, appApi, qn);
startParsing:
var respJson = JsonDocument.Parse(webJsonStr);
var data = respJson.RootElement;
//intl接口
if (webJsonStr.Contains("\"stream_list\""))
{
int pDur = data.GetProperty("data").GetProperty("video_info").GetProperty("timelength").GetInt32() / 1000;
var audio = data.GetProperty("data").GetProperty("video_info").GetProperty("dash_audio").EnumerateArray().ToList();
foreach(var stream in data.GetProperty("data").GetProperty("video_info").GetProperty("stream_list").EnumerateArray())
{
if (stream.TryGetProperty("dash_video", out JsonElement dashVideo))
{
if (dashVideo.GetProperty("base_url").ToString() != "")
{
var videoId = stream.GetProperty("stream_info").GetProperty("quality").ToString();
var urlList = new List<string>() { dashVideo.GetProperty("base_url").ToString() };
urlList.AddRange(dashVideo.GetProperty("backup_url").EnumerateArray().Select(i => i.ToString()));
Video v = new()
{
dur = pDur,
id = videoId,
dfn = Config.qualitys[videoId],
bandwith = Convert.ToInt64(dashVideo.GetProperty("bandwidth").ToString()) / 1000,
baseUrl = urlList.FirstOrDefault(i => !BaseUrlRegex().IsMatch(i), urlList.First()),
codecs = GetVideoCodec(dashVideo.GetProperty("codecid").ToString()),
size = dashVideo.TryGetProperty("size", out var sizeNode) ? Convert.ToDouble(sizeNode.ToString()) : 0
};
if (!videoTracks.Contains(v)) videoTracks.Add(v);
}
}
}
foreach(var node in audio)
{
var urlList = new List<string>() { node.GetProperty("base_url").ToString() };
urlList.AddRange(node.GetProperty("backup_url").EnumerateArray().Select(i => i.ToString()));
Audio a = new()
{
id = node.GetProperty("id").ToString(),
dfn = node.GetProperty("id").ToString(),
dur = pDur,
bandwith = Convert.ToInt64(node.GetProperty("bandwidth").ToString()) / 1000,
baseUrl = urlList.FirstOrDefault(i => !BaseUrlRegex().IsMatch(i), urlList.First()),
codecs = "M4A"
};
if (!audioTracks.Contains(a)) audioTracks.Add(a);
}
if (intlCode == "0")
{
intlCode = "1";
webJsonStr = await GetPlayJsonAsync(aid, cid, epId, qn, intlCode);
goto startParsing;
}
return (webJsonStr, videoTracks, audioTracks, clips, dfns, backgroundAudioTracks, roleAudioList, points);
}
// data节点一次性判断完
string nodeName = null;
if (webJsonStr.Contains("\"result\":{")) nodeName = "result";
else if (webJsonStr.Contains("\"data\":{")) nodeName = "data";
var root = nodeName == null ? data : data.GetProperty(nodeName);
bool bangumi = aidOri.StartsWith("ep:");
if (webJsonStr.Contains("\"dash\":{")) //dash
{
List<JsonElement>? audio = null;
List<JsonElement>? video = null;
List<JsonElement>? backgroundAudio = null;
List<JsonElement>? roleAudio = null;
int pDur = 0;
try { pDur = root.GetProperty("dash").GetProperty("duration").GetInt32(); } catch { }
try { pDur = root.GetProperty("timelength").GetInt32() / 1000; } catch { }
bool reParse = false;
reParse:
if (reParse)
{
webJsonStr = await GetPlayJsonAsync(encoding, aidOri, aid, cid, epId, tvApi, intlApi, appApi, GetMaxQn());
respJson = JsonDocument.Parse(webJsonStr);
data = respJson.RootElement;
root = nodeName == null ? data : data.GetProperty(nodeName);
}
try { video = root.GetProperty("dash").GetProperty("video").EnumerateArray().ToList(); } catch { }
try { audio = root.GetProperty("dash").GetProperty("audio").EnumerateArray().ToList(); } catch { }
if (appApi && bangumi)
{
try { backgroundAudio = data.GetProperty("dubbing_info").GetProperty("background_audio").EnumerateArray().ToList(); } catch { }
try { roleAudio = data.GetProperty("dubbing_info").GetProperty("role_audio_list").EnumerateArray().ToList(); } catch { }
}
//处理杜比音频
try
{
if (audio != null)
{
if (!tvApi && root.GetProperty("dash").TryGetProperty("dolby", out JsonElement dolby))
{
if (dolby.TryGetProperty("audio", out JsonElement db))
{
audio.AddRange(db.EnumerateArray());
}
}
}
}
catch (Exception) { ; }
//处理Hi-Res无损
try
{
if (audio != null)
{
if (!tvApi && root.GetProperty("dash").TryGetProperty("flac", out JsonElement hiRes))
{
if (hiRes.TryGetProperty("audio", out JsonElement db))
{
if (db.ValueKind != JsonValueKind.Null)
audio.Add(db);
}
}
}
}
catch (Exception) {; }
if (video != null)
{
foreach (var node in video)
{
var urlList = new List<string>() { node.GetProperty("base_url").ToString() };
if(node.TryGetProperty("backup_url", out JsonElement element) && element.ValueKind != JsonValueKind.Null)
{
urlList.AddRange(element.EnumerateArray().Select(i => i.ToString()));
}
var videoId = node.GetProperty("id").ToString();
Video v = new()
{
dur = pDur,
id = videoId,
dfn = Config.qualitys[videoId],
bandwith = Convert.ToInt64(node.GetProperty("bandwidth").ToString()) / 1000,
baseUrl = urlList.FirstOrDefault(i => !BaseUrlRegex().IsMatch(i), urlList.First()),
codecs = GetVideoCodec(node.GetProperty("codecid").ToString()),
size = node.TryGetProperty("size", out var sizeNode) ? Convert.ToDouble(sizeNode.ToString()) : 0
};
if (!tvApi && !appApi)
{
v.res = node.GetProperty("width").ToString() + "x" + node.GetProperty("height").ToString();
v.fps = node.GetProperty("frame_rate").ToString();
}
if (!videoTracks.Contains(v)) videoTracks.Add(v);
}
}
//此处处理免二压视频,需要单独再请求一次
if (!reParse && !appApi)
{
reParse = true;
goto reParse;
}
if (audio != null)
{
foreach (var node in audio)
{
var urlList = new List<string>() { node.GetProperty("base_url").ToString() };
if (node.TryGetProperty("backup_url", out JsonElement element) && element.ValueKind != JsonValueKind.Null)
{
urlList.AddRange(element.EnumerateArray().Select(i => i.ToString()));
}
var audioId = node.GetProperty("id").ToString();
var codecs = node.GetProperty("codecs").ToString();
codecs = codecs switch
{
"mp4a.40.2" => "M4A",
"mp4a.40.5" => "M4A",
"ec-3" => "E-AC-3",
"fLaC" => "FLAC",
_ => codecs
};
audioTracks.Add(new Audio()
{
id = audioId,
dfn = audioId,
dur = pDur,
bandwith = Convert.ToInt64(node.GetProperty("bandwidth").ToString()) / 1000,
baseUrl = urlList.FirstOrDefault(i => !BaseUrlRegex().IsMatch(i), urlList.First()),
codecs = codecs
});
}
}
if (backgroundAudio != null && roleAudio != null)
{
foreach (var node in backgroundAudio)
{
var audioId = node.GetProperty("id").ToString();
var urlList = new List<string> { node.GetProperty("base_url").ToString() };
urlList.AddRange(node.GetProperty("backup_url").EnumerateArray().Select(i => i.ToString()));
backgroundAudioTracks.Add(new Audio()
{
id = audioId,
dfn = audioId,
dur = pDur,
bandwith = Convert.ToInt64(node.GetProperty("bandwidth").ToString()) / 1000,
baseUrl = urlList.FirstOrDefault(i => !BaseUrlRegex().IsMatch(i), urlList.First()),
codecs = node.GetProperty("codecs").ToString()
});
}
foreach (var role in roleAudio)
{
var roleAudioTracks = new List<Audio>();
foreach (var node in role.GetProperty("audio").EnumerateArray())
{
var audioId = node.GetProperty("id").ToString();
var urlList = new List<string> { node.GetProperty("base_url").ToString() };
urlList.AddRange(node.GetProperty("backup_url").EnumerateArray().Select(i => i.ToString()));
roleAudioTracks.Add(new Audio()
{
id = audioId,
dfn = audioId,
dur = pDur,
bandwith = Convert.ToInt64(node.GetProperty("bandwidth").ToString()) / 1000,
baseUrl = urlList.FirstOrDefault(i => !BaseUrlRegex().IsMatch(i), urlList.First()),
codecs = node.GetProperty("codecs").ToString()
});
}
roleAudioList.Add(new AudioMaterialInfo()
{
title = role.GetProperty("title").ToString(),
personName = role.GetProperty("person_name").ToString(),
path = $"{aid}/{aid}.{cid}.{role.GetProperty("audio_id").ToString()}.m4a",
audio = roleAudioTracks
});
}
}
}
else if (webJsonStr.Contains("\"durl\":[")) //flv
{
//默认以最高清晰度解析
webJsonStr = await GetPlayJsonAsync(encoding, aidOri, aid, cid, epId, tvApi, intlApi, appApi, GetMaxQn());
data = JsonDocument.Parse(webJsonStr).RootElement;
root = nodeName == null ? data : data.GetProperty(nodeName);
string quality = "";
string videoCodecid = "";
string url = "";
double size = 0;
double length = 0;
quality = root.GetProperty("quality").ToString();
videoCodecid = root.GetProperty("video_codecid").ToString();
//获取所有分段
foreach (var node in root.GetProperty("durl").EnumerateArray())
{
clips.Add(node.GetProperty("url").ToString());
size += node.GetProperty("size").GetDouble();
length += node.GetProperty("length").GetDouble();
}
//TV模式可用清晰度
if (root.TryGetProperty("qn_extras", out JsonElement qnExtras))
{
dfns.AddRange(qnExtras.EnumerateArray().Select(node => node.GetProperty("qn").ToString()));
}
else if (root.TryGetProperty("accept_quality", out JsonElement acceptQuality)) //非tv模式可用清晰度
{
dfns.AddRange(acceptQuality.EnumerateArray()
.Select(node => node.ToString())
.Where(_qn => !string.IsNullOrEmpty(_qn)));
}
Video v = new()
{
id = quality,
dfn = Config.qualitys[quality],
baseUrl = url,
codecs = GetVideoCodec(videoCodecid),
dur = (int)length / 1000,
size = size
};
if (!videoTracks.Contains(v)) videoTracks.Add(v);
}
// 番剧片头片尾转分段信息, 预计效果: 正片? -> 片头 -> 正片 -> 片尾
if (bangumi)
{
if (root.TryGetProperty("clip_info_list", out JsonElement clipList))
{
points.AddRange(clipList.EnumerateArray().Select(clip => new ViewPoint()
{
title = clip.GetProperty("toastText").ToString().Replace("即将跳过", ""),
start = clip.GetProperty("start").GetInt32(),
end = clip.GetProperty("end").GetInt32()
})
);
points.Sort((p1, p2) => p1.start.CompareTo(p2.start));
var newPoints = new List<ViewPoint>();
int lastEnd = 0;
foreach (var point in points)
{
if (lastEnd < point.start)
newPoints.Add(new ViewPoint() { title = "正片", start = lastEnd, end = point.start });
newPoints.Add(point);
lastEnd = point.end;
}
points = newPoints;
}
}
return (webJsonStr, videoTracks, audioTracks, clips, dfns, backgroundAudioTracks, roleAudioList, points);
}
/// <summary>
/// 编码转换
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
private static string GetVideoCodec(string code)
{
return code switch
{
"13" => "AV1",
"12" => "HEVC",
"7" => "AVC",
_ => "UNKNOWN"
};
}
private static string GetMaxQn()
{
return Config.qualitys.Keys.First();
}
private static string GetTimeStamp(bool bflag)
{
DateTimeOffset ts = DateTimeOffset.Now;
return bflag ? ts.ToUnixTimeSeconds().ToString() : ts.ToUnixTimeMilliseconds().ToString();
}
private static string GetSign(string parms, bool isBiliPlus)
{
string toEncode = parms + (isBiliPlus ? "acd495b248ec528c2eed1e862d393126" : "59b43e04ad6965f34319062b478f83dd");
return string.Concat(MD5.HashData(Encoding.UTF8.GetBytes(toEncode)).Select(i => i.ToString("x2")).ToArray());
}
[GeneratedRegex("window.__playinfo__=([\\s\\S]*?)<\\/script>")]
private static partial Regex PlayerJsonRegex();
[GeneratedRegex("http.*:\\d+")]
private static partial Regex BaseUrlRegex();
}
}