Skip to content

Commit

Permalink
rc2
Browse files Browse the repository at this point in the history
  • Loading branch information
nilaoda committed Aug 9, 2020
1 parent 408b312 commit 3b725af
Show file tree
Hide file tree
Showing 14 changed files with 1,586 additions and 1 deletion.
25 changes: 25 additions & 0 deletions BBDown.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29519.181
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BBDown", "BBDown\BBDown.csproj", "{0D491417-BFEA-481B-AE1A-3BE69172D818}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0D491417-BFEA-481B-AE1A-3BE69172D818}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0D491417-BFEA-481B-AE1A-3BE69172D818}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0D491417-BFEA-481B-AE1A-3BE69172D818}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0D491417-BFEA-481B-AE1A-3BE69172D818}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {17E08D9B-5969-4301-9674-ADB3EA8E5DF1}
EndGlobalSection
EndGlobal
15 changes: 15 additions & 0 deletions BBDown/BBDown.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Microsoft.DotNet.ILCompiler" Version="1.0.0-alpha-*" />
<PackageReference Include="QRCoder" Version="1.3.9" />
<PackageReference Include="SharpZipLib" Version="1.2.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20371.2" />
</ItemGroup>
</Project>
4 changes: 4 additions & 0 deletions BBDown/BBDown.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
61 changes: 61 additions & 0 deletions BBDown/BBDownEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace BBDown
{
class BBDownEntity
{
public struct Page
{
public int index;
public string cid;
public string title;
public int dur;
public string res;

public Page(int index, string cid, string title, int dur, string res)
{
this.index = index;
this.cid = cid;
this.title = title;
this.dur = dur;
this.res = res;
}
}

public struct Video
{
public string id;
public string dfn;
public string baseUrl;
public string res;
public string fps;
public string codecs;
public long bandwith;
}

public struct Audio
{
public string id;
public string dfn;
public string baseUrl;
public string codecs;
public long bandwith;
}

public struct Subtitle
{
public string lan;
public string url;
public string path;
}

public struct Clip
{
public int index;
public long from;
public long to;
}
}
}
35 changes: 35 additions & 0 deletions BBDown/BBDownLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace BBDown
{
class BBDownLogger
{
public static void Log(string text, bool enter = true)
{
Console.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss.fff]") + " - " + text);
if (enter) Console.WriteLine();
}

public static void LogError(string text)
{
Console.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss.fff]") + " - ");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(text);
Console.ResetColor();
}

public static void LogColor(string text, bool time = true)
{
if (time)
Console.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss.fff]") + " - ");
Console.ForegroundColor = ConsoleColor.Cyan;
if (time)
Console.WriteLine(text);
else
Console.WriteLine(" " + text);
Console.ResetColor();
}
}
}
69 changes: 69 additions & 0 deletions BBDown/BBDownMuxer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using static BBDown.BBDownEntity;
using static BBDown.BBDownSubUtil;
using static BBDown.BBDownLogger;

namespace BBDown
{
class BBDownMuxer
{
public static int MuxAV(string videoPath, string audioPath, string outPath, string desc = "", string title = "", string episodeId = "", string pic = "", List<Subtitle> subs = null)
{
//----分析并生成-i参数
StringBuilder inputArg = new StringBuilder();
StringBuilder metaArg = new StringBuilder();
if (!string.IsNullOrEmpty(videoPath))
inputArg.Append($" -i \"{videoPath}\" ");
if (!string.IsNullOrEmpty(audioPath))
inputArg.Append($" -i \"{audioPath}\" ");
if (!string.IsNullOrEmpty(pic))
inputArg.Append($" -i \"{pic}\" ");
if (subs != null)
{
for (int i = 0; i < subs.Count; i++)
{
inputArg.Append($" -i \"{subs[i].path}\" ");
metaArg.Append($" -metadata:s:s:{i} handler_name=\"{SubDescDic[subs[i].lan]}\" -metadata:s:s:{i} language={SubLangDic[subs[i].lan]} ");
}
}
if (!string.IsNullOrEmpty(pic))
metaArg.Append(" -disposition:v:1 attached_pic ");
var inputCount = Regex.Matches(inputArg.ToString(), "-i \"").Count;
for (int i = 0; i < inputCount; i++)
{
inputArg.Append($" -map {i} ");
}

//----分析完毕
int code = 0;
Process p = new Process();
p.StartInfo.FileName = "ffmpeg";
p.StartInfo.Arguments = $"-loglevel warning -y " +
inputArg.ToString() + metaArg.ToString() + $" -metadata title=\"" + (episodeId == "" ? title : episodeId) + "\" " +
$"-metadata description=\"{desc}\" " +
(episodeId == "" ? "" : $"-metadata album=\"{title}\" ") +
$"-c copy " +
(subs != null ? " -c:s mov_text " : "") +
$"\"{outPath}\"";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = false;
p.ErrorDataReceived += delegate (object sendProcess, DataReceivedEventArgs output) {
if (!string.IsNullOrWhiteSpace(output.Data))
Log(output.Data);
};
p.StartInfo.StandardErrorEncoding = Encoding.UTF8;
p.Start();
p.BeginErrorReadLine();
p.WaitForExit();
p.Close();
p.Dispose();
return code;
}

}
}
30 changes: 30 additions & 0 deletions BBDown/BBDownParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using static BBDown.BBDownUtil;

namespace BBDown
{
class BBDownParser
{
public static string GetPlayJson(string aid, string cid, string epId, bool tvApi, bool bangumi, string qn = "0")
{
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") +
"&fnver=0&fnval=16" + (tvApi ? "&device=android&platform=android&mobi_app=android_tv_yst&npcybs=0&force_host=0&build=102801" : "") +
(bangumi ? $"&module=bangumi&ep_id={epId}&fourk=1" + "&session=" : "");
//Console.WriteLine(api);
string webJson = GetWebSource(api);
//以下情况从网页源代码尝试解析
if (webJson.Contains("\"大会员专享限制\""))
{
string webUrl = "https://www.bilibili.com/bangumi/play/ep" + epId;
string webSource = GetWebSource(webUrl);
webJson = Regex.Match(webSource, @"window.__playinfo__=([\s\S]*?)<\/script>").Groups[1].Value;
}
return webJson;
}
}
}
145 changes: 145 additions & 0 deletions BBDown/BBDownSubUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using static BBDown.BBDownUtil;

namespace BBDown
{
class BBDownSubUtil
{
public static void SaveSubtitle(string url, string path)
{
File.WriteAllText(path, ConvertSubFromJson(GetWebSource(url)), new UTF8Encoding());
}

private static string ConvertSubFromJson(string jsonString)
{
StringBuilder lines = new StringBuilder();
JObject json = JObject.Parse(jsonString);
JArray sub = JArray.Parse(json["body"].ToString());
for(int i = 0; i < sub.Count; i++)
{
lines.AppendLine((i + 1).ToString());
lines.AppendLine($"{FormatTime(sub[i]["from"].ToString())} --> {FormatTime(sub[i]["to"].ToString())}");
lines.AppendLine(sub[i]["content"].ToString());
lines.AppendLine();
}
return lines.ToString();
}

private static string FormatTime(string sec) //64.13
{
string[] v = { sec, "" };
if (sec.Contains("."))
v = sec.Split('.');
v[1] = v[1].PadRight(3, '0').Substring(0, 3);
int secs = Convert.ToInt32(v[0]);
TimeSpan ts = new TimeSpan(0, 0, secs);
string str = "";
str = ts.Hours.ToString("00") + ":" + ts.Minutes.ToString("00") + ":" + ts.Seconds.ToString("00") + "," + v[1];
return str;
}

public static Dictionary<string, string> SubDescDic = new Dictionary<string, string>
{
{"ar", "العربية"}, {"ar-eg", "العربية"},
{"bg", "български"}, {"cmn-hans", "国语 (简体)"},
{"cmn-hant", "國語 (繁體)"}, {"cs", "čeština"},
{"da", "Dansk"}, {"da-dk", "Dansk"},
{"de", "Deutsch"}, {"de-de", "Deutsch"},
{"el", "Ελληνικά"}, {"en", "English"},
{"en-US", "English"}, {"es", "Español (Latinoamérica)"},
{"es-419", "Español (Latinoamérica)"}, {"es-es", "Español (España)"},
{"es-ES", "Español (España)"}, {"fi", "Suomi"},
{"fi-fi", "Suomi"}, {"fr", "Français"},
{"fr-fr", "Français"}, {"he", "עברית"},
{"he-il", "עברית"}, {"hi", "हिन्दी"},
{"hi-in", "हिन्दी"}, {"hr", "Hrvatska"},
{"id", "Indonesia"}, {"id-id", "Indonesia"},
{"it", "Italiano"}, {"it-it", "Italiano"},
{"ja", "日本語"}, {"ja-ja", "日本語"},
{"jp", "日本語"}, {"jp-jp", "日本語"},
{"ko", "한국어"}, {"ko-kr", "한국어"},
{"ms", "Melayu"}, {"nb", "Norsk Bokmål"},
{"nb-no", "Norsk Bokmål"}, {"nl", "Nederlands"},
{"nl-BE", "Nederlands"}, {"nl-be", "Nederlands"},
{"nl-nl", "Nederlands"}, {"nob", "norsk"},
{"pl", "Polski"}, {"pl-pl", "Polski"},
{"pt", "Português"}, {"pt-BR", "Português"},
{"pt-br", "Português"}, {"ro", "Română"},
{"ru", "Русский"}, {"ru-ru", "Русский"},
{"sk", "slovenský"}, {"sv", "Svenska"},
{"sv-se", "Svenska"}, {"ta-in", "தமிழ்"},
{"te-in", "తెలుగు"}, {"th", "ไทย"},
{"tl", "Tagalog"}, {"tr", "Türkçe"},
{"tr-tr", "Türkçe"}, {"uk", "Українська"},
{"vi", "Tiếng Việt"}, {"zxx", "zxx"},
{"zh-hans", "中文(简体)"},
{"zh-Hans", "中文(简体)"},
{"zh-CN", "中文(简体)"},
{"zh-TW", "中文(繁體)"},
{"zh-HK", "中文(繁體)"},
{"zh-MO", "中文(繁體)"},
{"zh-Hant", "中文(繁體)"},
{"zh-hant", "中文(繁體)"},
{"yue", "中文(粤语)"},
{"hu", "Magyar"},
{"et", "Eestlane"}, {"bn", "বাংলা ভাষার"},
{"iw", "שפה עברית"}, {"sr", "српски језик"},
{"hy", "հայերեն"}, {"az", "Azərbaycan"},
{"kk", "Қазақ тілі"}, {"is", "icelandic"},
{"fil", "Pilipino"}, {"ku", "Kurdî"},
{"ca", "català"}, {"no", "norsk språk"}
};

public static Dictionary<string, string> SubLangDic = new Dictionary<string, string>
{
{"ar","ara"}, {"ar-eg","ara"},
{"bg","bul"}, {"cmn-hans","chi"},
{"cmn-hant","chi"}, {"cs","cze"},
{"da","dan"}, {"da-dk","dan"},
{"de","ger"}, {"de-de","ger"},
{"el","gre"}, {"en","eng"},
{"en-US","eng"}, {"es","spa"},
{"es-419","spa"}, {"es-ES","spa"},
{"es-es","spa"}, {"fi","fin"},
{"fi-fi","fin"}, {"fr","fre"},
{"fr-fr","fre"}, {"he","heb"},
{"he-il","heb"}, {"hi","hin"},
{"hi-in","hin"}, {"hr","hrv"},
{"id","ind"}, {"id-id","ind"},
{"it","ita"}, {"it-it","ita"},
{"ja","jpn"}, {"ja-ja","jpn"},
{"jp","jpn"}, {"jp-jp","jpn"},
{"ko","kor"}, {"ko-kr","kor"},
{"ms","may"}, {"nb","nor"},
{"nb-no","nor"}, {"nl","dut"},
{"nl-BE","dut"}, {"nl-be","dut"},
{"nl-nl","dut"}, {"nob","nor"},
{"pl","pol"}, {"pl-pl","pol"},
{"pt","por"}, {"pt-BR","por"},
{"pt-br","por"}, {"ro","rum"},
{"ru","rus"}, {"ru-ru","rus"},
{"sk","slo"}, {"sv","swe"},
{"sv-se","swe"}, {"ta-in","tam"},
{"te-in","tel"}, {"th","tha"},
{"tl","tgl"}, {"tr","tur"},
{"tr-tr","tur"}, {"uk","ukr"},
{"vi","vie"}, {"zh-hans","chi"},
{"zh-Hans","chi"}, {"zh-Hant","chi"},
{"zh-hant","chi"}, {"zh-CN","chi"},
{"zh-TW","chi"}, {"zh-HK","chi"},
{"zh-MO","chi"}, {"zh-CHS","chi"},
{"zh-CHT","chi"}, {"zh-SG","chi"},
{"et", "est"}, {"bn", "ben"},
{"iw", "heb"}, {"sr", "srp"},
{"hy", "arm"}, {"az", "aze"},
{"kk", "kaz"}, {"is", "ice"},
{"fil", "phi"}, {"ku", "kur"},
{"ca", "cat"}, {"no", "nor"},
{"hu", "hun"}
};
}
}
Loading

0 comments on commit 3b725af

Please sign in to comment.