Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 对#668 的初步实现 #710

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion BBDown.Core/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
namespace BBDown.Core
using System.Text;

namespace BBDown.Core
{
public class Logger
{
public static readonly string debugFileName = "debug.json";

public static void Log(object text, bool enter = true)
{
Console.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss.fff]") + " - " + text);
Expand Down Expand Up @@ -57,5 +61,28 @@ public static void LogDebug(string toFormat, params object[] args)
Console.WriteLine();
}
}

public static void WriteDebugFile(string content)
{
if (!Config.DEBUG_LOG)
return;
FileStream fs;
if (File.Exists(debugFileName))
{
using (fs = File.OpenWrite(debugFileName))
{
fs.SetLength(0);
}
}
else
{
fs = File.Create(debugFileName);
}
using (fs)
using (var writer = new StreamWriter(fs, Encoding.UTF8))
{
writer.Write(content);
}
}
}
}
35 changes: 13 additions & 22 deletions BBDown/BBDownMuxer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ private static string EscapeString(string str)
return string.IsNullOrEmpty(str) ? str : str.Replace("\"", "'").Replace("\\", "\\\\");
}

private static int MuxByMp4box(string videoPath, string audioPath, string outPath, string desc, string title, string author, string episodeId, string pic, string lang, List<Subtitle>? subs, bool audioOnly, bool videoOnly, List<ViewPoint>? points)
private static int MuxByMp4box(string? videoPath, string? audioPath, string outPath, string? desc, string? title, string? author, string? episodeId, string? pic, string lang, List<Subtitle>? subs, List<ViewPoint>? points)
{
StringBuilder inputArg = new();
StringBuilder metaArg = new();
int nowId = 0;
inputArg.Append(" -inter 500 -noprog ");
if (!string.IsNullOrEmpty(videoPath))
{
inputArg.Append($" -add \"{videoPath}#trackID={(audioOnly && audioPath == "" ? "2" : "1")}:name=\" ");
inputArg.Append($" -add \"{videoPath}#trackID=1:name=\" ");
nowId++;
}
if (!string.IsNullOrEmpty(audioPath))
Expand Down Expand Up @@ -95,28 +95,24 @@ private static int MuxByMp4box(string videoPath, string audioPath, string outPat
return RunExe(MP4BOX, arguments, MP4BOX != "mp4box");
}

public static int MuxAV(bool useMp4box, string videoPath, string audioPath, List<AudioMaterial> audioMaterial, string outPath, string desc = "", string title = "", string author = "", string episodeId = "", string pic = "", string lang = "", List<Subtitle>? subs = null, bool audioOnly = false, bool videoOnly = false, List<ViewPoint>? points = null, long pubTime = 0)
public static int MuxAV(bool useMp4box, string? videoPath, string? audioPath, List<AudioMaterial>? audioMaterial, string outPath, string desc = "", string title = "", string author = "", string episodeId = "", string pic = "", string lang = "", List<Subtitle>? subs = null, List<ViewPoint>? points = null, long pubTime = 0)
{
if (audioOnly && audioPath != "")
videoPath = "";
if (videoOnly)
audioPath = "";
desc = EscapeString(desc);
title = EscapeString(title);
episodeId = EscapeString(episodeId);

if (useMp4box)
{
return MuxByMp4box(videoPath, audioPath, outPath, desc, title, author, episodeId, pic, lang, subs, audioOnly, videoOnly, points);
return MuxByMp4box(videoPath, audioPath, outPath, desc, title, author, episodeId, pic, lang, subs, points);
}

if (outPath.Contains('/') && ! Directory.Exists(Path.GetDirectoryName(outPath)))
if (outPath.Contains('/') && !Directory.Exists(Path.GetDirectoryName(outPath)))
Directory.CreateDirectory(Path.GetDirectoryName(outPath)!);
//----分析并生成-i参数
StringBuilder inputArg = new();
StringBuilder metaArg = new();
byte inputCount = 0;
foreach (string path in new string[] { videoPath, audioPath })
foreach (string? path in new[] { videoPath, audioPath })
{
if (!string.IsNullOrEmpty(path))
{
Expand All @@ -139,12 +135,6 @@ public static int MuxAV(bool useMp4box, string videoPath, string audioPath, List
}
}

if (!string.IsNullOrEmpty(pic))
{
inputCount++;
inputArg.Append($"-i \"{pic}\" ");
}

if (subs != null)
{
for (int i = 0; i < subs.Count; i++)
Expand All @@ -153,13 +143,14 @@ public static int MuxAV(bool useMp4box, string videoPath, string audioPath, List
{
inputCount++;
inputArg.Append($"-i \"{subs[i].path}\" ");
metaArg.Append($"-metadata:s:s:{i} title=\"{GetSubtitleCode(subs[i].lan).Item2}\" -metadata:s:s:{i} language={GetSubtitleCode(subs[i].lan).Item1} ");
metaArg.Append($"-metadata:s:s:{i} title=\"{GetSubtitleCode(subs[i].lan).Item2}\" ");
metaArg.Append($"-metadata:s:s:{i} language={GetSubtitleCode(subs[i].lan).Item1} ");
}
}
}

if (!string.IsNullOrEmpty(pic))
metaArg.Append($"-disposition:v:{(audioOnly ? "0" : "1")} attached_pic ");
if (pic != null)
metaArg.Append($"-disposition:v:{(videoPath == null ? "0" : "1")} attached_pic ");
// var inputCount = InputRegex().Matches(inputArg.ToString()).Count;

if (points != null && points.Any())
Expand All @@ -183,10 +174,10 @@ public static int MuxAV(bool useMp4box, string videoPath, string audioPath, List
if (!string.IsNullOrEmpty(author)) argsBuilder.Append($"-metadata artist=\"{author}\" ");
if (episodeId != "") argsBuilder.Append($"-metadata album=\"{title}\" ");
if (pubTime != 0) argsBuilder.Append($"-metadata creation_time=\"{(DateTimeOffset.FromUnixTimeSeconds(pubTime).ToString("yyyy-MM-ddTHH:mm:ss.ffffffZ"))}\" ");
argsBuilder.Append("-c copy ");
if (audioOnly && audioPath == "") argsBuilder.Append("-vn ");
if (outPath.EndsWith(".mp4") || outPath.EndsWith(".m4a")) argsBuilder.Append("-c copy ");
if (videoPath == null) argsBuilder.Append("-vn ");
if (subs != null) argsBuilder.Append("-c:s mov_text ");
argsBuilder.Append($"-movflags faststart -strict unofficial -strict -2 -f mp4 -- \"{outPath}\"");
argsBuilder.Append($"-movflags faststart -strict unofficial -strict -2 -- \"{outPath}\"");

string arguments = argsBuilder.ToString();

Expand Down
4 changes: 4 additions & 0 deletions BBDown/BBDownUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,10 @@ public static async Task<bool> CheckLogin(string cookie)
}
}

public static double CalculateSize(int duration, long bandwith) => duration * bandwith * 1024 / 8;
public static double CalculateSize(Video video) => video.size > 0 ? video.size : CalculateSize(video.dur, video.bandwith);
public static double CalculateSize(Audio audio) => CalculateSize(audio.dur, audio.bandwith);

[GeneratedRegex("av(\\d+)")]
private static partial Regex AvRegex();
[GeneratedRegex("[Bb][Vv](\\w+)")]
Expand Down
27 changes: 3 additions & 24 deletions BBDown/CommandLineInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,10 @@ internal class CommandLineInvoker
private readonly static Option<string> Aria2cArgs = new(new string[] { "--aria2c-args" }, "调用aria2c的附加参数(默认参数包含\"-x16 -s16 -j16 -k 5M\", 使用时注意字符串转义)");
private readonly static Option<bool> MultiThread = new(new string[] { "--multi-thread", "-mt" }, "使用多线程下载(默认开启)");
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 或 -p 3,5,LATEST)");
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<string> DownloadOnly = new(new string[] { "--only" }, "仅下载部分内容,'a'->音频,'v'->视频,'c'->封面,‘s’->字幕,'d'->弹幕,用例: --only avsc");
private readonly static Option<bool> Debug = new(new string[] { "--debug" }, "输出调试日志");
private readonly static Option<bool> SkipMux = new(new string[] { "--skip-mux" }, "跳过混流步骤");
private readonly static Option<bool> SkipSubtitle = new(new string[] { "--skip-subtitle" }, "跳过字幕下载");
private readonly static Option<bool> SkipCover = new(new string[] { "--skip-cover" }, "跳过封面下载");
private readonly static Option<bool> ForceHttp = new(new string[] { "--force-http" }, "下载音视频时强制使用HTTP协议替换HTTPS(默认开启)");
private readonly static Option<bool> DownloadDanmaku = new(new string[] { "--download-danmaku", "-dd" }, "下载弹幕");
private readonly static Option<bool> SkipAi = new(new string[] { "--skip-ai" }, description: "跳过AI字幕下载(默认开启)");
private readonly static Option<bool> VideoAscending = new(new string[] { "--video-ascending" }, "视频升序(最小体积优先)");
private readonly static Option<bool> AudioAscending = new(new string[] { "--audio-ascending" }, "音频升序(最小体积优先)");
Expand Down Expand Up @@ -88,17 +81,10 @@ protected override MyOption GetBoundValue(BindingContext bindingContext)
if (bindingContext.ParseResult.HasOption(Interactive)) option.Interactive = bindingContext.ParseResult.GetValueForOption(Interactive)!;
if (bindingContext.ParseResult.HasOption(HideStreams)) option.HideStreams = bindingContext.ParseResult.GetValueForOption(HideStreams)!;
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(CoverOnly)) option.CoverOnly = bindingContext.ParseResult.GetValueForOption(CoverOnly)!;
if (bindingContext.ParseResult.HasOption(SubOnly)) option.SubOnly = bindingContext.ParseResult.GetValueForOption(SubOnly)!;
if (bindingContext.ParseResult.HasOption(DownloadOnly)) option.DownloadOnly = bindingContext.ParseResult.GetValueForOption(DownloadOnly)!;
if (bindingContext.ParseResult.HasOption(Debug)) option.Debug = bindingContext.ParseResult.GetValueForOption(Debug)!;
if (bindingContext.ParseResult.HasOption(SkipMux)) option.SkipMux = bindingContext.ParseResult.GetValueForOption(SkipMux)!;
if (bindingContext.ParseResult.HasOption(SkipSubtitle)) option.SkipSubtitle = bindingContext.ParseResult.GetValueForOption(SkipSubtitle)!;
if (bindingContext.ParseResult.HasOption(SkipCover)) option.SkipCover = bindingContext.ParseResult.GetValueForOption(SkipCover)!;
if (bindingContext.ParseResult.HasOption(ForceHttp)) option.ForceHttp = bindingContext.ParseResult.GetValueForOption(ForceHttp)!;
if (bindingContext.ParseResult.HasOption(DownloadDanmaku)) option.DownloadDanmaku = bindingContext.ParseResult.GetValueForOption(DownloadDanmaku)!;
if (bindingContext.ParseResult.HasOption(SkipAi)) option.SkipAi = bindingContext.ParseResult.GetValueForOption(SkipAi)!;
if (bindingContext.ParseResult.HasOption(VideoAscending)) option.VideoAscending = bindingContext.ParseResult.GetValueForOption(VideoAscending)!;
if (bindingContext.ParseResult.HasOption(AudioAscending)) option.AudioAscending = bindingContext.ParseResult.GetValueForOption(AudioAscending)!;
Expand Down Expand Up @@ -150,24 +136,17 @@ public static RootCommand GetRootCommand(Func<MyOption, Task> action)
Interactive,
HideStreams,
MultiThread,
VideoOnly,
AudioOnly,
DanmakuOnly,
SubOnly,
CoverOnly,
Debug,
SkipMux,
SkipSubtitle,
SkipCover,
ForceHttp,
DownloadDanmaku,
SkipAi,
VideoAscending,
AudioAscending,
AllowPcdn,
FilePattern,
MultiFilePattern,
SelectPage,
DownloadOnly,
Language,
UserAgent,
Cookie,
Expand Down
9 changes: 1 addition & 8 deletions BBDown/MyOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,16 @@ internal class MyOption
public bool UseMP4box { get; set; }
public string? EncodingPriority { get; set; }
public string? DfnPriority { get; set; }
public string DownloadOnly { get; set; } = "avsc";
public bool OnlyShowInfo { get; set; }
public bool ShowAll { get; set; }
public bool UseAria2c { get; set; }
public bool Interactive { get; set; }
public bool HideStreams { get; set; }
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; }
public bool SkipSubtitle { get; set; }
public bool SkipCover { get; set; }
public bool ForceHttp { get; set; } = true;
public bool DownloadDanmaku { get; set; } = false;
public bool SkipAi { get; set; } = true;
public bool VideoAscending { get; set; } = false;
public bool AudioAscending { get; set; } = false;
Expand Down
Loading