Skip to content

Commit

Permalink
支持仅下载封面 #408
Browse files Browse the repository at this point in the history
  • Loading branch information
nilaoda committed May 26, 2023
1 parent 2f31d1b commit bd1a6d0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
3 changes: 3 additions & 0 deletions BBDown/CommandLineInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal class CommandLineInvoker
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> CoverOnly = new(new string[] { "--cover-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 @@ -85,6 +86,7 @@ protected override MyOption GetBoundValue(BindingContext bindingContext)
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(CoverOnly)) option.CoverOnly = bindingContext.ParseResult.GetValueForOption(CoverOnly)!;
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 @@ -142,6 +144,7 @@ public static RootCommand GetRootCommand(Func<MyOption, Task> action)
AudioOnly,
DanmakuOnly,
SubOnly,
CoverOnly,
Debug,
SkipMux,
SkipSubtitle,
Expand Down
2 changes: 2 additions & 0 deletions BBDown/MyOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ internal class MyOption
public bool MultiThread { get; set; } = true;
public bool VideoOnly { get; set; }
public bool AudioOnly { get; set; }
public bool DanmakuOnly { get; set; }
public bool CoverOnly { get; set; }
public bool SubOnly { get; set; }
public bool Debug { get; set; }
public bool SkipMux { get; set; }
Expand Down
35 changes: 24 additions & 11 deletions BBDown/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ private static async Task DoWorkAsync(MyOption myOption)
bool audioOnly = myOption.AudioOnly;
bool videoOnly = myOption.VideoOnly;
bool danmakuOnly = myOption.DanmakuOnly;
bool coverOnly = myOption.CoverOnly;
bool subOnly = myOption.SubOnly;
bool skipMux = myOption.SkipMux;
bool skipSubtitle = myOption.SkipSubtitle;
Expand Down Expand Up @@ -487,20 +488,12 @@ private static async Task DoWorkAsync(MyOption myOption)
{
Directory.CreateDirectory(p.aid);
}
if (!skipCover && !subOnly && !File.Exists(coverPath) && !danmakuOnly)
if (!skipCover && !subOnly && !File.Exists(coverPath) && !danmakuOnly && !coverOnly)
{
Log("下载封面...");
var cover = pic == "" ? p.cover : pic;
if (cover != null)
{
LogDebug("下载:{0}", cover);
await using var response = await HTTPUtil.AppHttpClient.GetStreamAsync(cover);
await using var fs = new FileStream(coverPath, FileMode.Create);
await response.CopyToAsync(fs);
}
await DownloadCoverAsync(pic, p, coverPath);
}

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

if (coverOnly)
{
var newCoverPath = savePath[..savePath.LastIndexOf('.')] + Path.GetExtension(pic);
await DownloadCoverAsync(pic, p, newCoverPath);
continue;
}

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

private static async Task DownloadCoverAsync(string pic, Page p, string coverPath)
{
Log("下载封面...");
var cover = pic == "" ? p.cover : pic;
if (cover != null)
{
LogDebug("下载:{0}", cover);
await using var response = await HTTPUtil.AppHttpClient.GetStreamAsync(cover);
await using var fs = new FileStream(coverPath, FileMode.Create);
await response.CopyToAsync(fs);
}
}

private static List<Video> SortTracks(List<Video> videoTracks, Dictionary<string, int> dfnPriority, Dictionary<string, byte> encodingPriority, bool bandwithAscending)
{
//用户同时输入了自定义分辨率优先级和自定义编码优先级, 则根据输入顺序依次进行排序
Expand Down

0 comments on commit bd1a6d0

Please sign in to comment.