Skip to content

Commit

Permalink
Merge pull request nilaoda#661 from My-Responsitories/master
Browse files Browse the repository at this point in the history
改进了app端的解析
  • Loading branch information
nilaoda authored Jul 30, 2023
2 parents 0d3d38e + 1e98384 commit ccd41de
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
1 change: 1 addition & 0 deletions BBDown.Core/APP/Payload/playviewreq.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ message PlayViewReq {
NOCODE = 0;
CODE264 = 1;
CODE265 = 2;
CODEAV1 = 3;
}
optional CodeType preferCodecType = 12;
optional bool isPreview = 13;
Expand Down
29 changes: 22 additions & 7 deletions BBDown.Core/AppHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ class AppHelper
private static readonly string region = "CN";
private static readonly string language = "zh";

private static PlayViewReq.Types.CodeType GetVideoCodeType(string code)
{
return code switch
{
"AVC" => PlayViewReq.Types.CodeType.Code264,
"HEVC" => PlayViewReq.Types.CodeType.Code265,
"AV1" => PlayViewReq.Types.CodeType.Codeav1,
_ => PlayViewReq.Types.CodeType.Code265
};
}

/// <summary>
/// 发起请求并返回响应报文(protobuf -> json)
/// </summary>
Expand All @@ -41,11 +52,11 @@ class AppHelper
/// <param name="qn"></param>
/// <param name="appkey"></param>
/// <returns></returns>
public static async Task<string> DoReqAsync(string aid, string cid, string epId, string qn, bool bangumi, bool onlyAvc, string appkey = "")
public static async Task<string> DoReqAsync(string aid, string cid, string epId, string qn, bool bangumi, string encoding, string appkey = "")
{
var headers = GetHeader(appkey);
LogDebug("App-Req-Headers: {0}", JsonSerializer.Serialize(headers, JsonContext.Default.DictionaryStringString));
var body = GetPayload(Convert.ToInt64(aid), Convert.ToInt64(cid), Convert.ToInt64(qn), onlyAvc ? PlayViewReq.Types.CodeType.Code264 : PlayViewReq.Types.CodeType.Code265);
var body = GetPayload(Convert.ToInt64(aid), Convert.ToInt64(cid), Convert.ToInt64(qn), GetVideoCodeType(encoding));
//Console.WriteLine(ReadMessage<PlayViewReq>(body));
var data = await GetPostResponseAsync(API, body, headers);
PlayViewReply? resp;
Expand All @@ -62,7 +73,7 @@ public static async Task<string> DoReqAsync(string aid, string cid, string epId,

if (bangumi)
{
body = GetPayload(Convert.ToInt64(epId), Convert.ToInt64(cid), Convert.ToInt64(qn), onlyAvc ? PlayViewReq.Types.CodeType.Code264 : PlayViewReq.Types.CodeType.Code265);
body = GetPayload(Convert.ToInt64(epId), Convert.ToInt64(cid), Convert.ToInt64(qn), GetVideoCodeType(encoding));
data = await GetPostResponseAsync(API2, body, headers);
resp = new MessageParser<PlayViewReply>(() => new PlayViewReply()).ParseFrom(ReadMessage(data));
}
Expand Down Expand Up @@ -95,6 +106,7 @@ private static string ConvertToDashJson(object data)
videos.Add(new AudioInfoWitCodecId(
item.StreamInfo.Quality,
item.DashVideo.BaseUrl,
item.DashVideo.BackupUrl.ToList(),
(uint)(item.DashVideo.Size * 8 / (resp.VideoInfo.Timelength / 1000)),
item.DashVideo.Codecid
));
Expand Down Expand Up @@ -150,8 +162,8 @@ private static byte[] GetPayload(long aid, long cid, long qn, PlayViewReq.Types.
EpId = aid,
Cid = cid,
//obj.Qn = qn;
Qn = 126,
Fnval = 976,
Qn = 127,
Fnval = 4048,
Spmid = "main.ugc-video-detail.0.0",
FromSpmid = "main.my-history.0.0",
PreferCodecType = codec,
Expand Down Expand Up @@ -378,7 +390,7 @@ internal class AudioInfoWithCodecName
[JsonPropertyName("codecs")]
public string Codecs { get; }

public AudioInfoWithCodecName(uint id, string base_url,List<string> backup_url, uint bandwidth, string codecs)
public AudioInfoWithCodecName(uint id, string base_url, List<string> backup_url, uint bandwidth, string codecs)
{
Id = id;
BaseUrl = base_url;
Expand All @@ -397,15 +409,18 @@ internal class AudioInfoWitCodecId
public uint Id { get; }
[JsonPropertyName("base_url")]
public string BaseUrl { get; }
[JsonPropertyName("backup_url")]
public List<string> BackupUrl { get; }
[JsonPropertyName("bandwidth")]
public uint Bandwidth { get; }
[JsonPropertyName("codecid")]
public uint Codecid { get; }

public AudioInfoWitCodecId(uint id, string base_url, uint bandwidth, uint codecid)
public AudioInfoWitCodecId(uint id, string base_url, List<string> backup_url, uint bandwidth, uint codecid)
{
Id = id;
BaseUrl = base_url;
BackupUrl = backup_url;
Bandwidth = bandwidth;
Codecid = codecid;
}
Expand Down
20 changes: 6 additions & 14 deletions BBDown.Core/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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(bool onlyAvc, string aidOri, string aid, string cid, string epId, bool tvApi, bool intl, bool appApi, string qn = "0")
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);

Expand All @@ -26,7 +26,7 @@ private static async Task<string> GetPlayJsonAsync(bool onlyAvc, string aidOri,
bool bangumi = cheese || aidOri.StartsWith("ep:");
LogDebug("bangumi={0},cheese={1}", bangumi, cheese);

if (appApi) return await AppHelper.DoReqAsync(aid, cid, epId, qn, bangumi, onlyAvc, Config.TOKEN);
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";
Expand Down Expand Up @@ -85,13 +85,7 @@ private static async Task<string> GetPlayJsonAsync(string aid, string cid, strin
return webJson;
}

public static string SkiPcdn(List<string> urlList, string baseUrl)
{
urlList.Add(baseUrl);
return urlList.FirstOrDefault(i => !PcdnRegex().IsMatch(i), urlList.First());
}

public static async Task<(string, List<Video>, List<Audio>, List<string>, List<string>)> ExtractTracksAsync(string aidOri, string aid, string cid, string epId, bool tvApi, bool intlApi, bool appApi, string qn = "0")
public static async Task<(string, List<Video>, List<Audio>, List<string>, List<string>)> 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();
Expand All @@ -100,7 +94,7 @@ public static string SkiPcdn(List<string> urlList, string baseUrl)
var intlCode = "0";

//调用解析
string webJsonStr = await GetPlayJsonAsync(false, aidOri, aid, cid, epId, tvApi, intlApi, appApi, qn);
string webJsonStr = await GetPlayJsonAsync(encoding, aidOri, aid, cid, epId, tvApi, intlApi, appApi, qn);

startParsing:
var respJson = JsonDocument.Parse(webJsonStr);
Expand Down Expand Up @@ -179,7 +173,7 @@ public static string SkiPcdn(List<string> urlList, string baseUrl)
reParse:
if (reParse)
{
webJsonStr = await GetPlayJsonAsync(false, aidOri, aid, cid, epId, tvApi, intlApi, appApi, GetMaxQn());
webJsonStr = await GetPlayJsonAsync(encoding, aidOri, aid, cid, epId, tvApi, intlApi, appApi, GetMaxQn());
respJson = JsonDocument.Parse(webJsonStr);
}
try { video = !tvApi ? respJson.RootElement.GetProperty(nodeName).GetProperty("dash").GetProperty("video").EnumerateArray().ToList() : respJson.RootElement.GetProperty("dash").GetProperty("video").EnumerateArray().ToList(); } catch { }
Expand Down Expand Up @@ -289,7 +283,7 @@ public static string SkiPcdn(List<string> urlList, string baseUrl)
else if (webJsonStr.Contains("\"durl\":[")) //flv
{
//默认以最高清晰度解析
webJsonStr = await GetPlayJsonAsync(false, aidOri, aid, cid, epId, tvApi, intlApi, appApi, GetMaxQn());
webJsonStr = await GetPlayJsonAsync(encoding, aidOri, aid, cid, epId, tvApi, intlApi, appApi, GetMaxQn());
respJson = JsonDocument.Parse(webJsonStr);
string quality = "";
string videoCodecid = "";
Expand Down Expand Up @@ -419,7 +413,5 @@ private static string GetSign(string parms, bool isBiliPlus)
private static partial Regex PlayerJsonRegex();
[GeneratedRegex("http.*:\\d+")]
private static partial Regex BaseUrlRegex();
[GeneratedRegex("://.*:\\d+/")]
private static partial Regex PcdnRegex();
}
}
6 changes: 4 additions & 2 deletions BBDown/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,12 @@ private static async Task DoWorkAsync(MyOption myOption)
bool useMp4box = myOption.UseMP4box;

var encodingPriority = new Dictionary<string, byte>();
string firstEncoding = "";
if (myOption.EncodingPriority != null)
{
var encodingPriorityTemp = myOption.EncodingPriority.Replace(",", ",").Split(',').Select(s => s.ToUpper().Trim()).Where(s => !string.IsNullOrEmpty(s));
byte index = 0;
firstEncoding = encodingPriorityTemp.First();
foreach (string encoding in encodingPriorityTemp)
{
if (encodingPriority.ContainsKey(encoding)) { continue; }
Expand Down Expand Up @@ -579,7 +581,7 @@ private static async Task DoWorkAsync(MyOption myOption)
}

//调用解析
(webJsonStr, videoTracks, audioTracks, clips, dfns) = await ExtractTracksAsync(aidOri, p.aid, p.cid, p.epid, tvApi, intlApi, appApi);
(webJsonStr, videoTracks, audioTracks, clips, dfns) = await ExtractTracksAsync(aidOri, p.aid, p.cid, p.epid, tvApi, intlApi, appApi, firstEncoding);

if (Config.DEBUG_LOG)
File.WriteAllText($"debug.json", webJsonStr);
Expand Down Expand Up @@ -873,7 +875,7 @@ private static async Task DoWorkAsync(MyOption myOption)
Console.ResetColor();
//重新解析
videoTracks.Clear();
(webJsonStr, videoTracks, audioTracks, clips, dfns) = await ExtractTracksAsync(aidOri, p.aid, p.cid, p.epid, tvApi, intlApi, appApi, dfns[vIndex]);
(webJsonStr, videoTracks, audioTracks, clips, dfns) = await ExtractTracksAsync(aidOri, p.aid, p.cid, p.epid, tvApi, intlApi, appApi, firstEncoding, dfns[vIndex]);
flag = true;
selected = true;
goto reParse;
Expand Down

0 comments on commit ccd41de

Please sign in to comment.