Skip to content

Commit

Permalink
仅下载弹幕 #572
Browse files Browse the repository at this point in the history
  • Loading branch information
nilaoda committed May 26, 2023
1 parent a359a92 commit 7c082a3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
3 changes: 3 additions & 0 deletions BBDown/CommandLineInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal class CommandLineInvoker
private readonly static Option<string> SelectPage = new(new string[] { "--select-page", "-p" }, "选择指定分p或分p范围: (-p 8 或 -p 1,2 或 -p 3-5 或 -p ALL 或 -p LAST)");
private readonly static Option<bool> AudioOnly = new(new string[] { "--audio-only" }, "仅下载音频");
private readonly static Option<bool> VideoOnly = new(new string[] { "--video-only" }, "仅下载视频");
private readonly static Option<bool> DanmakuOnly = new(new string[] { "--danmaku-only" }, "仅下载弹幕");
private readonly static Option<bool> SubOnly = new(new string[] { "--sub-only" }, "仅下载字幕");
private readonly static Option<bool> Debug = new(new string[] { "--debug" }, "输出调试日志");
private readonly static Option<bool> SkipMux = new(new string[] { "--skip-mux" }, "跳过混流步骤");
Expand Down Expand Up @@ -83,6 +84,7 @@ protected override MyOption GetBoundValue(BindingContext bindingContext)
if (bindingContext.ParseResult.HasOption(MultiThread)) option.MultiThread = bindingContext.ParseResult.GetValueForOption(MultiThread)!;
if (bindingContext.ParseResult.HasOption(VideoOnly)) option.VideoOnly = bindingContext.ParseResult.GetValueForOption(VideoOnly)!;
if (bindingContext.ParseResult.HasOption(AudioOnly)) option.AudioOnly = bindingContext.ParseResult.GetValueForOption(AudioOnly)!;
if (bindingContext.ParseResult.HasOption(DanmakuOnly)) option.DanmakuOnly = bindingContext.ParseResult.GetValueForOption(DanmakuOnly)!;
if (bindingContext.ParseResult.HasOption(SubOnly)) option.SubOnly = bindingContext.ParseResult.GetValueForOption(SubOnly)!;
if (bindingContext.ParseResult.HasOption(Debug)) option.Debug = bindingContext.ParseResult.GetValueForOption(Debug)!;
if (bindingContext.ParseResult.HasOption(SkipMux)) option.SkipMux = bindingContext.ParseResult.GetValueForOption(SkipMux)!;
Expand Down Expand Up @@ -138,6 +140,7 @@ public static RootCommand GetRootCommand(Func<MyOption, Task> action)
MultiThread,
VideoOnly,
AudioOnly,
DanmakuOnly,
SubOnly,
Debug,
SkipMux,
Expand Down
64 changes: 37 additions & 27 deletions BBDown/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,13 @@ private static async Task DoWorkAsync(MyOption myOption)
bool multiThread = myOption.MultiThread;
bool audioOnly = myOption.AudioOnly;
bool videoOnly = myOption.VideoOnly;
bool danmakuOnly = myOption.DanmakuOnly;
bool subOnly = myOption.SubOnly;
bool skipMux = myOption.SkipMux;
bool skipSubtitle = myOption.SkipSubtitle;
bool skipCover = myOption.SkipCover;
bool forceHttp = myOption.ForceHttp;
bool downloadDanmaku = myOption.DownloadDanmaku;
bool downloadDanmaku = myOption.DownloadDanmaku || danmakuOnly;
bool skipAi = myOption.SkipAi;
bool bandwithAscending = myOption.BandwithAscending;
bool showAll = myOption.ShowAll;
Expand Down Expand Up @@ -486,7 +487,7 @@ private static async Task DoWorkAsync(MyOption myOption)
{
Directory.CreateDirectory(p.aid);
}
if (!skipCover && !subOnly && !File.Exists(coverPath))
if (!skipCover && !subOnly && !File.Exists(coverPath) && !danmakuOnly)
{
Log("下载封面...");
var cover = pic == "" ? p.cover : pic;
Expand All @@ -499,7 +500,7 @@ private static async Task DoWorkAsync(MyOption myOption)
}
}

if (!skipSubtitle)
if (!skipSubtitle && !danmakuOnly)
{
LogDebug("获取字幕...");
subtitleInfo = await SubUtil.GetSubtitlesAsync(p.aid, p.cid, p.epid, p.index, intlApi);
Expand Down Expand Up @@ -590,6 +591,39 @@ private static async Task DoWorkAsync(MyOption myOption)
}
}
if (infoMode) continue;

LogDebug("Format Before: " + savePathFormat);
savePath = FormatSavePath(savePathFormat, title, videoTracks.ElementAtOrDefault(vIndex), audioTracks.ElementAtOrDefault(aIndex), p, pagesCount, tvApi, appApi, intlApi);
LogDebug("Format After: " + savePath);

if (downloadDanmaku)
{
var danmakuXmlPath = savePath[..savePath.LastIndexOf('.')] + ".xml";
var danmakuAssPath = savePath[..savePath.LastIndexOf('.')] + ".ass";
Log("正在下载弹幕Xml文件");
string danmakuUrl = "https://comment.bilibili.com/" + p.cid + ".xml";
await DownloadFile(danmakuUrl, danmakuXmlPath, false, aria2cArgs);
var danmakus = DanmakuUtil.ParseXml(danmakuXmlPath);
if (danmakus != null)
{
Log("正在保存弹幕Ass文件...");
await DanmakuUtil.SaveAsAssAsync(danmakus, danmakuAssPath);
}
else
{
Log("弹幕Xml解析失败, 删除Xml...");
File.Delete(danmakuXmlPath);
}
if (danmakuOnly)
{
if (Directory.Exists(p.aid))
{
Directory.Delete(p.aid);
}
continue;
}
}

if (interactMode && !hideStreams && !selected)
{
if (videoTracks.Count > 0)
Expand Down Expand Up @@ -662,30 +696,6 @@ private static async Task DoWorkAsync(MyOption myOption)
}
}

LogDebug("Format Before: " + savePathFormat);
savePath = FormatSavePath(savePathFormat, title, videoTracks.ElementAtOrDefault(vIndex), audioTracks.ElementAtOrDefault(aIndex), p, pagesCount, tvApi, appApi, intlApi);
LogDebug("Format After: " + savePath);

if (downloadDanmaku)
{
var danmakuXmlPath = savePath[..savePath.LastIndexOf('.')] + ".xml";
var danmakuAssPath = savePath[..savePath.LastIndexOf('.')] + ".ass";
Log("正在下载弹幕Xml文件");
string danmakuUrl = "https://comment.bilibili.com/" + p.cid + ".xml";
await DownloadFile(danmakuUrl, danmakuXmlPath, false, aria2cArgs);
var danmakus = DanmakuUtil.ParseXml(danmakuXmlPath);
if (danmakus != null)
{
Log("正在保存弹幕Ass文件...");
await DanmakuUtil.SaveAsAssAsync(danmakus, danmakuAssPath);
}
else
{
Log("弹幕Xml解析失败, 删除Xml...");
File.Delete(danmakuXmlPath);
}
}

if (videoTracks.Count > 0)
{

Expand Down

0 comments on commit 7c082a3

Please sign in to comment.