Skip to content

Commit

Permalink
添加 字幕下载 & Release 2.1.7
Browse files Browse the repository at this point in the history
此次的契机是缓存 LexBurner 直播恶心发言的字幕. 同时对于 LexBurner 的发言,我个人表示十分厌恶,已经取关了.
贴个知乎链接: https://www.zhihu.com/question/442911026
  • Loading branch information
kengwang committed Feb 5, 2021
1 parent fcbc534 commit 24f28d6
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 75 deletions.
4 changes: 3 additions & 1 deletion BiliDuang/BiliDuang.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="DanmakuAss.cs" />
<Compile Include="JSONCallback\SubPlayer.cs" />
<Compile Include="tools\Bcc2srt.cs" />
<Compile Include="tools\DanmakuAss.cs" />
<Compile Include="DownloadObject.cs" />
<Compile Include="DownloadQueue.cs" />
<Compile Include="JSONCallback\Cheese.cs" />
Expand Down
127 changes: 75 additions & 52 deletions BiliDuang/DownloadObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ public void LinkStart()
single = true;
}

DownloadDanmaku();
if (Settings.downloaddanmaku)
DownloadDanmaku();
if (Settings.downloadcc)
DownloadSubtitle();

if (type == 0)
{
Expand Down Expand Up @@ -389,64 +392,83 @@ private void Completed(bool complete, string msg)
}
}

private void DownloadSubtitle(string sid = "")
{
string playerback = Encoding.UTF8.GetString(new WebClient().DownloadData(string.Format("https://api.bilibili.com/x/player/v2?cid={0}&aid={1}", cid, aid)));
JSONCallback.SubPlayer.Root playerbackjson = JsonConvert.DeserializeObject<JSONCallback.SubPlayer.Root>(playerback);
if (playerbackjson.code != 0)
{
Console.WriteLine("下载字幕出错");
return;
}
string bcc = "";
if (playerbackjson.data.subtitle.subtitles.Count == 0) return;
if (playerbackjson.data.subtitle.subtitles.FindIndex((x) => { return x.id == sid; }) != -1)
{
bcc = Encoding.UTF8.GetString(new WebClient().DownloadData("https:" + playerbackjson.data.subtitle.subtitles.Find((x) => { return x.id == sid; }).subtitle_url));
}
else
{
bcc = Encoding.UTF8.GetString(new WebClient().DownloadData("https:" + playerbackjson.data.subtitle.subtitles[0].subtitle_url));

}
File.WriteAllText(saveto + "/" + avname + ".srt", Bcc2srt.Convert(bcc));
}

private void DownloadDanmaku()
{
if (Settings.downloaddanmaku)
try
{
try
message = "正在下载弹幕";
//1.'https://comment.bilibili.com/' + cid + '.xml'
//2.'https://api.bilibili.com/x/v1/dm/list.so?oid=' + cid
string danmakuorigin = Other.GetHtml("https://comment.bilibili.com/" + cid + ".xml");
//暂时存一下原始弹幕
File.WriteAllText(saveto + "/" + avname + ".xml", danmakuorigin);
XmlDocument xml = new XmlDocument();
xml.LoadXml(danmakuorigin);
if (xml.GetElementsByTagName("state")[0].InnerText != "0")
{
message = "正在下载弹幕";
//1.'https://comment.bilibili.com/' + cid + '.xml'
//2.'https://api.bilibili.com/x/v1/dm/list.so?oid=' + cid
string danmakuorigin = Other.GetHtml("https://comment.bilibili.com/" + cid + ".xml");
//暂时存一下原始弹幕
File.WriteAllText(saveto + "/" + avname + ".xml", danmakuorigin);
XmlDocument xml = new XmlDocument();
xml.LoadXml(danmakuorigin);
if (xml.GetElementsByTagName("state")[0].InnerText != "0")
{
//弹幕出错
message = "弹幕下载出错";
}
else
//弹幕出错
message = "弹幕下载出错";
}
else
{
XmlNodeList xmlNodeList = xml.GetElementsByTagName("d");
if (urls[0].width == 0)
{
XmlNodeList xmlNodeList = xml.GetElementsByTagName("d");
if (urls[0].width == 0)
switch (quality)
{
switch (quality)
{
case 120://4K
urls[0].width = 4096;
urls[0].height = 2160;
break;
case 116://1080P60
case 112://1080P+
case 80://1080P
urls[0].width = 1920;
urls[0].height = 1080;
break;
case 74://720P60
case 64://720P
urls[0].width = 1280;
urls[0].height = 720;
break;
case 32://480P
urls[0].width = 720;
urls[0].height = 480;
break;
case 16://360P
urls[0].width = 480;
urls[0].height = 360;
break;
}
case 120://4K
urls[0].width = 4096;
urls[0].height = 2160;
break;
case 116://1080P60
case 112://1080P+
case 80://1080P
urls[0].width = 1920;
urls[0].height = 1080;
break;
case 74://720P60
case 64://720P
urls[0].width = 1280;
urls[0].height = 720;
break;
case 32://480P
urls[0].width = 720;
urls[0].height = 480;
break;
case 16://360P
urls[0].width = 480;
urls[0].height = 360;
break;
}
string assdmk = DanmakuAss.DanmakuAss.Convert(xmlNodeList, urls[0].width, urls[0].height);
File.WriteAllText(saveto + "/" + avname + ".ass", assdmk);
}
string assdmk = DanmakuAss.DanmakuAss.Convert(xmlNodeList, urls[0].width, urls[0].height);
File.WriteAllText(saveto + "/" + avname + ".ass", assdmk);
}
catch (Exception) { }
}

catch (Exception) { }

}

Expand Down Expand Up @@ -667,7 +689,7 @@ private bool GetDownloadUrls()
}
catch (Exception e)
{
message = "链接数据解析失败: "+e.Message;
message = "链接数据解析失败: " + e.Message;
return false;
}
if (player.code == -404)
Expand Down Expand Up @@ -835,13 +857,14 @@ private bool GetDownloadUrls()
}

}
}catch (Exception e)
}
catch (Exception e)
{
Console.WriteLine("下载链接获取出错: " + e.ToString());
message = "获取下载链接出错";
return false;
}

}

#region Aria2c下载
Expand Down
84 changes: 84 additions & 0 deletions BiliDuang/JSONCallback/SubPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BiliDuang.JSONCallback.SubPlayer
{

public class SubtitlesItem
{
/// <summary>
///
/// </summary>
public string id { get; set; }
/// <summary>
///
/// </summary>
public string lan { get; set; }
/// <summary>
/// 中文(中国)
/// </summary>
public string lan_doc { get; set; }
/// <summary>
///
/// </summary>
public string is_lock { get; set; }
/// <summary>
///
/// </summary>
public int author_mid { get; set; }
/// <summary>
///
/// </summary>
public string subtitle_url { get; set; }
}

public class Subtitle
{
/// <summary>
///
/// </summary>
public string allow_submit { get; set; }
/// <summary>
///
/// </summary>
public string lan { get; set; }
/// <summary>
///
/// </summary>
public string lan_doc { get; set; }
/// <summary>
///
/// </summary>
public List<SubtitlesItem> subtitles { get; set; }
}

public class Data
{

public Subtitle subtitle { get; set; }
}

public class Root
{
/// <summary>
///
/// </summary>
public int code { get; set; }
/// <summary>
///
/// </summary>
public string message { get; set; }
/// <summary>
///
/// </summary>
public int ttl { get; set; }
/// <summary>
///
/// </summary>
public Data data { get; set; }
}

}
22 changes: 21 additions & 1 deletion BiliDuang/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions BiliDuang/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ private void Initialize()
Tabs.Size = new Size(Tabs.Width, Tabs.Height + 30);
materialCheckBox1.Checked = Settings.usearia2c;
materialCheckBox2.Checked = Settings.downloaddanmaku;
materialCheckBox4.Checked = Settings.downloadcc;
aria2cargu.Visible = materialCheckBox1.Checked;
aria2cargu.Text = Settings.aria2cargument;
materialFlatButton7.Visible = materialCheckBox1.Checked;
Expand Down Expand Up @@ -523,5 +524,11 @@ private void materialCheckBox3_CheckedChanged(object sender, EventArgs e)
{
Settings.thailandphrase = materialCheckBox3.Checked;
}

private void materialCheckBox4_CheckedChanged(object sender, EventArgs e)
{
Settings.downloadcc = materialCheckBox4.Checked;
Settings.SaveSettings();
}
}
}
8 changes: 6 additions & 2 deletions BiliDuang/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace BiliDuang
{
internal class Settings
{
public static string versionCode = "2.1.6";
public static string versionName = "Arks";
public static string versionCode = "2.1.7";
public static string versionName = "Staroshi";

public static int maxMission = 1;
public static int useapi = 0; //0 - Bilibili 1 - BiliPlus 2 - BiliBili TV
Expand All @@ -18,6 +18,7 @@ internal class Settings
public static bool usearia2c = false;
public static bool thailandphrase = false;
public static bool downloaddanmaku = false;
public static bool downloadcc = false;
public static string aria2cargument = "";

public static void SaveSettings()
Expand All @@ -32,6 +33,7 @@ public static void SaveSettings()
usearia2c = Settings.usearia2c,
aria2cargument = Settings.aria2cargument,
downloaddanmaku = Settings.downloaddanmaku,
downloadcc = Settings.downloadcc,
apilink = Settings.apilink
};
File.WriteAllText(Environment.CurrentDirectory + "/config/settings", JsonConvert.SerializeObject(settings));
Expand All @@ -50,6 +52,7 @@ public static void ReadSettings()
Settings.usearia2c = setting.usearia2c;
Settings.aria2cargument = setting.aria2cargument;
Settings.downloaddanmaku = setting.downloaddanmaku;
Settings.downloadcc = setting.downloadcc;
Settings.apilink = setting.apilink;
}
catch (Exception)
Expand All @@ -68,6 +71,7 @@ internal class _Settings
public bool autodark = true;
public bool usearia2c = false;
public bool downloaddanmaku = false;
public bool downloadcc = false;
public string aria2cargument = "";
internal string apilink;
}
Expand Down
Loading

0 comments on commit 24f28d6

Please sign in to comment.