Skip to content

Commit

Permalink
v1.2.4
Browse files Browse the repository at this point in the history
修复部分番剧视频分P重复问题
增加--debug选项
细节优化
  • Loading branch information
nilaoda committed Sep 13, 2020
1 parent 3e37243 commit d995eee
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 8 deletions.
2 changes: 1 addition & 1 deletion BBDown/BBDown.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>1.2.3</Version>
<Version>1.2.4</Version>
<Description>BBDown是一个免费且便捷高效的哔哩哔哩下载/解析软件.</Description>
</PropertyGroup>

Expand Down
14 changes: 14 additions & 0 deletions BBDown/BBDownLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace BBDown
{
class BBDownLogger
{
public static bool DEBUG_LOG = false;

public static void Log(string text, bool enter = true)
{
Console.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss.fff]") + " - " + text);
Expand Down Expand Up @@ -33,5 +35,17 @@ public static void LogColor(string text, bool time = true)
Console.ResetColor();
Console.WriteLine();
}

public static void LogDebug(string toFormat, params object[] args)
{
if (DEBUG_LOG)
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss.fff]") + " - ");
Console.Write(string.Format(toFormat, args).Trim());
Console.ResetColor();
Console.WriteLine();
}
}
}
}
5 changes: 4 additions & 1 deletion BBDown/BBDownMuxer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static int ffmpeg(string parms)
return code;
}

public static int MuxAV(string videoPath, string audioPath, string outPath, string desc = "", string title = "", string episodeId = "", string pic = "", List<Subtitle> subs = null)
public static int MuxAV(string videoPath, string audioPath, string outPath, string desc = "", string title = "", string episodeId = "", string pic = "", List<Subtitle> subs = null, bool audioOnly = false, bool videoOnly = false)
{
//----分析并生成-i参数
StringBuilder inputArg = new StringBuilder();
Expand Down Expand Up @@ -67,9 +67,11 @@ public static int MuxAV(string videoPath, string audioPath, string outPath, stri
inputArg.ToString() + metaArg.ToString() + $" -metadata title=\"" + (episodeId == "" ? title : episodeId) + "\" " +
$"-metadata description=\"{desc}\" " +
(episodeId == "" ? "" : $"-metadata album=\"{title}\" ") +
(audioOnly ? " -vn " : "") + (videoOnly ? " -an " : "") +
$"-c copy " +
(subs != null ? " -c:s mov_text " : "") +
$"\"{outPath}\"";
LogDebug("ffmpeg命令:{0}", arguments);
return ffmpeg(arguments);
}

Expand All @@ -85,6 +87,7 @@ public static void MergeFLV(string[] files, string outPath)
{
var tmpFile = Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file) + ".ts");
var arguments = $"-loglevel warning -y -i \"{file}\" -map 0 -c copy -f mpegts -bsf:v h264_mp4toannexb \"{tmpFile}\"";
LogDebug("ffmpeg命令:{0}", arguments);
ffmpeg(arguments);
File.Delete(file);
}
Expand Down
2 changes: 2 additions & 0 deletions BBDown/BBDownParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
using System.Text;
using System.Text.RegularExpressions;
using static BBDown.BBDownUtil;
using static BBDown.BBDownLogger;

namespace BBDown
{
class BBDownParser
{
public static string GetPlayJson(string aid, string cid, string epId, bool tvApi, bool bangumi, string qn = "0")
{
LogDebug("aid={0},cid={1},epId={2},tvApi={3},bangumi={4},qn={5}", aid, cid, epId, tvApi, bangumi, qn);
string prefix = tvApi ? (bangumi ? "api.snm0516.aisee.tv/pgc/player/api/playurltv" : "api.snm0516.aisee.tv/x/tv/ugc/playurl")
: (bangumi ? "api.bilibili.com/pgc/player/web/playurl" : "api.bilibili.com/x/player/playurl");
string api = $"https://{prefix}?avid={aid}&cid={cid}&qn={qn}&type=&otype=json" + (tvApi ? "" : "&fourk=1") +
Expand Down
4 changes: 4 additions & 0 deletions BBDown/BBDownUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Threading.Tasks;
using System.Web;
using static BBDown.BBDownEntity;
using static BBDown.BBDownLogger;

namespace BBDown
{
Expand Down Expand Up @@ -131,6 +132,7 @@ public static string GetWebSource(String url)
webRequest.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
webRequest.KeepAlive = false;
webRequest.AllowAutoRedirect = true; //自动跳转
LogDebug("获取网页内容:Url: {0}, Headers: {1}", url, webRequest.Headers);

HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
if (webResponse.ContentEncoding != null
Expand Down Expand Up @@ -229,8 +231,10 @@ await Task.WhenAll(activeTasks.ToArray()).ContinueWith(t =>
public static async Task MultiThreadDownloadFileAsync(string url, string path)
{
long fileSize = GetFileSize(url);
LogDebug("文件大小:{0} bytes", fileSize);
List<Clip> allClips = GetAllClips(url, fileSize);
int total = allClips.Count;
LogDebug("分段数量:{0}", total);
long done = 0;
using (var progress = new ProgressBar())
{
Expand Down
47 changes: 42 additions & 5 deletions BBDown/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class Program
public static string COOKIE = "";
public static string TOKEN = "";
static Dictionary<string, string> qualitys = new Dictionary<string, string>() {
{"120","超清 4K" }, {"116","高清 1080P60" },{"112","高清 1080P+" },
{"80","高清 1080P" }, {"74","高清 720P60" },{"64","高清 720P" },
{"48","高清 720P" }, {"32","清晰 480P" },{"16","流畅 360P" }
{"120","4K 超清" }, {"116","1080P60 高帧率" },{"112","1080P 高码率" },
{"80","1080P 高清" }, {"74","720P60 高帧率" },{"64","720P 高清" },
{"48","720P 高清" }, {"32","480P 清晰" },{"16","360P 流畅" }
};

private static int Compare(Video r1, Video r2)
Expand All @@ -49,9 +49,17 @@ class MyOption
public bool Interactive { get; set; }
public bool HideStreams { get; set; }
public bool MultiThread { get; set; }
public bool VideoOnly { get; set; }
public bool AudioOnly { get; set; }
public bool Debug { get; set; }
public string SelectPage { get; set; }
public string Cookie { get; set; }
public string AccessToken { get; set; }

public override string ToString()
{
return $"{{Input={Url}, {nameof(UseTvApi)}={UseTvApi.ToString()}, {nameof(OnlyHevc)}={OnlyHevc.ToString()}, {nameof(OnlyShowInfo)}={OnlyShowInfo.ToString()}, {nameof(Interactive)}={Interactive.ToString()}, {nameof(HideStreams)}={HideStreams.ToString()}, {nameof(MultiThread)}={MultiThread.ToString()}, {nameof(VideoOnly)}={VideoOnly.ToString()}, {nameof(AudioOnly)}={AudioOnly.ToString()}, {nameof(Debug)}={Debug.ToString()}, {nameof(SelectPage)}={SelectPage}, {nameof(Cookie)}={Cookie}, {nameof(AccessToken)}={AccessToken}}}";
}
}

public static int Main(params string[] args)
Expand Down Expand Up @@ -88,6 +96,15 @@ public static int Main(params string[] args)
new Option<string>(
new string[]{ "--select-page" ,"-p"},
"选择指定分p或分p范围"),
/*new Option<bool>(
new string[]{ "--audio-only" ,"-vn"},
"仅下载音频"),
new Option<bool>(
new string[]{ "--video-only" ,"-an"},
"仅下载视频"),*/
new Option<bool>(
new string[]{ "--debug"},
"输出调试日志"),
new Option<string>(
new string[]{ "--cookie" ,"-c"},
"设置字符串cookie用以下载网页接口的会员内容"),
Expand Down Expand Up @@ -218,7 +235,6 @@ public static int Main(params string[] args)

rootCommand.Handler = CommandHandler.Create<MyOption>(async (myOption) =>
{
//Console.WriteLine(myOption.ToString());
await DoWorkAsync(myOption);
});

Expand All @@ -243,26 +259,40 @@ private static async Task DoWorkAsync(MyOption myOption)
bool hevc = myOption.OnlyHevc;
bool hideStreams = myOption.HideStreams;
bool multiThread = myOption.MultiThread;
bool audioOnly = myOption.AudioOnly;
bool videoOnly = myOption.VideoOnly;
DEBUG_LOG = myOption.Debug;
string input = myOption.Url;
string selectPage = myOption.SelectPage;
string aid = "";
COOKIE = myOption.Cookie;
TOKEN = myOption.AccessToken != null ? myOption.AccessToken.Replace("access_token=", "") : "";

//audioOnly和videoOnly同时开启则全部忽视
if (audioOnly && videoOnly)
{
audioOnly = false;
videoOnly = false;
}

List<string> selectedPages = null;
if (!string.IsNullOrEmpty(GetQueryString("p", input)))
{
selectedPages = new List<string>();
selectedPages.Add(GetQueryString("p", input));
}

LogDebug("运行参数:{0}", myOption);
if (File.Exists(Path.Combine(AppContext.BaseDirectory, "BBDown.data")) && !tvApi)
{
Log("加载本地cookie...");
LogDebug("文件路径:{0}", Path.Combine(AppContext.BaseDirectory, "BBDown.data"));
COOKIE = File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "BBDown.data"));
}
if (File.Exists(Path.Combine(AppContext.BaseDirectory, "BBDownTV.data")) && tvApi)
{
Log("加载本地token...");
LogDebug("文件路径:{0}", Path.Combine(AppContext.BaseDirectory, "BBDownTV.data"));
TOKEN = File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "BBDownTV.data"));
}
Log("获取aid...");
Expand Down Expand Up @@ -299,9 +329,9 @@ private static async Task DoWorkAsync(MyOption myOption)

if (string.IsNullOrEmpty(aid)) throw new Exception("输入有误");
string api = $"https://api.bilibili.com/x/web-interface/view?aid={aid}";
Log("获取视频信息...");
string json = GetWebSource(api);
JObject infoJson = JObject.Parse(json);
Log("获取视频信息...");
string title = infoJson["data"]["title"].ToString();
string desc = infoJson["data"]["desc"].ToString();
string pic = infoJson["data"]["pic"].ToString();
Expand Down Expand Up @@ -331,10 +361,12 @@ private static async Task DoWorkAsync(MyOption myOption)
Directory.CreateDirectory(aid);
}
Log("下载封面...");
LogDebug("下载:{0}", pic);
new WebClient().DownloadFile(pic, $"{aid}/{aid}.jpg");
foreach (Subtitle s in subtitleInfo)
{
Log($"下载字幕 {s.lan}...");
LogDebug("下载:{0}", s.url);
BBDownSubUtil.SaveSubtitle(s.url, s.path);
}
}
Expand All @@ -344,7 +376,11 @@ private static async Task DoWorkAsync(MyOption myOption)
if (infoJson["data"]["redirect_url"].ToString().Contains("bangumi"))
{
bangumi = true;
LogDebug("获取epId...");
epId = Regex.Match(infoJson["data"]["redirect_url"].ToString(), "ep(\\d+)").Groups[1].Value;
//番剧内容通常不会有分P,如果有分P则不需要epId参数
if (pages.Count > 1) epId = "";
LogDebug("获取epId结束: {0}", epId);
}
}
catch { }
Expand Down Expand Up @@ -700,6 +736,7 @@ private static async Task DoWorkAsync(MyOption myOption)
else
{
LogError("解析此分P失败");
LogDebug("{0}", webJson);
continue;
}
}
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,16 @@ Commands:
<details>
<summary>点击查看详情</summary>

* 2020年9月13日 17:03
修复部分番剧视频分P重复问题
增加--debug选项
细节优化

* 2020年8月25日 22:36
修复免二压视频无法获取最高清晰度的问题

* 2020年8月19日 21:18
修复1.2.1中TV模式下没有获取到可用最高清晰度的问题
修复1.2.1中TV模式下没有获取到可用最高清晰度的问题

* 2020年8月18日 21:19
SSL处理
Expand Down

0 comments on commit d995eee

Please sign in to comment.