Skip to content

Commit

Permalink
元数据写入视频URL (#958)
Browse files Browse the repository at this point in the history
  • Loading branch information
nilaoda authored Nov 24, 2024
1 parent 5423f19 commit c5d97c2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
13 changes: 8 additions & 5 deletions BBDown/BBDownMuxer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ 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 url, 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)
{
StringBuilder inputArg = new();
StringBuilder metaArg = new();
Expand Down Expand Up @@ -73,7 +73,8 @@ private static int MuxByMp4box(string videoPath, string audioPath, string outPat
metaArg.Append($":album=\"{title}\":title=\"{episodeId}\"");
else
metaArg.Append($":title=\"{title}\"");
metaArg.Append($":comment=\"{desc}\"");
metaArg.Append($":sdesc=\"{desc}\"");
metaArg.Append($":comment=\"{url}\"");
metaArg.Append($":artist=\"{author}\"");

if (subs != null)
Expand All @@ -90,12 +91,12 @@ private static int MuxByMp4box(string videoPath, string audioPath, string outPat
}

//----分析完毕
var arguments = (Config.DEBUG_LOG ? " -v " : "") + inputArg + (metaArg.ToString() == "" ? "" : " -itags tool=" + metaArg.ToString()) + $" -new -- \"{outPath}\"";
var arguments = (Config.DEBUG_LOG ? " -v " : "") + inputArg + (metaArg.ToString() == "" ? "" : " -itags tool=" + metaArg) + $" -new -- \"{outPath}\"";
LogDebug("mp4box命令: {0}", arguments);
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, bool simplyMux = false)
public static int MuxAV(bool useMp4box, string bvid, 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, bool simplyMux = false)
{
if (audioOnly && audioPath != "")
videoPath = "";
Expand All @@ -104,10 +105,11 @@ public static int MuxAV(bool useMp4box, string videoPath, string audioPath, List
desc = EscapeString(desc);
title = EscapeString(title);
episodeId = EscapeString(episodeId);
var url = $"https://www.bilibili.com/video/{bvid}/";

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

if (outPath.Contains('/') && ! Directory.Exists(Path.GetDirectoryName(outPath)))
Expand Down Expand Up @@ -179,6 +181,7 @@ public static int MuxAV(bool useMp4box, string videoPath, string audioPath, List
argsBuilder.Append(metaArg);
if (!simplyMux) {
argsBuilder.Append($"-metadata title=\"{(episodeId == "" ? title : episodeId)}\" ");
argsBuilder.Append($"-metadata comment=\"{url}\" ");
if (lang != "") argsBuilder.Append($"-metadata:s:a:0 language={lang} ");
if (!string.IsNullOrWhiteSpace(desc)) argsBuilder.Append($"-metadata description=\"{desc}\" ");
if (!string.IsNullOrEmpty(author)) argsBuilder.Append($"-metadata artist=\"{author}\" ");
Expand Down
11 changes: 8 additions & 3 deletions BBDown/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public static (Dictionary<string, byte> encodingPriority, Dictionary<string, int

Log("获取aid...");
aidOri = await GetAvIdAsync(input);
Log("获取aid结束: " + aidOri);
Log($"获取aid结束: {aidOri}");

if (string.IsNullOrEmpty(aidOri))
{
Expand Down Expand Up @@ -287,6 +287,11 @@ public static (Dictionary<string, byte> encodingPriority, Dictionary<string, int
{
Log("发布时间: " + FormatTimeStamp(pubTime, "yyyy-MM-dd HH:mm:ss zzz"));
}
var bvid = vInfo.PagesInfo.FirstOrDefault()?.bvid;
if (!string.IsNullOrEmpty(bvid))
{
Log($"视频URL: https://www.bilibili.com/video/{bvid}/");
}
var mid = vInfo.PagesInfo.FirstOrDefault(p => !string.IsNullOrEmpty(p.ownerMid))?.ownerMid;
if (!string.IsNullOrEmpty(mid))
{
Expand Down Expand Up @@ -664,7 +669,7 @@ private static async Task DownloadPageAsync(Page p, MyOption myOption, VInfo vIn
Log($"开始合并音视频{(subtitleInfo.Any() ? "和字幕" : "")}...");
if (myOption.AudioOnly)
savePath = savePath[..^4] + ".m4a";
int code = BBDownMuxer.MuxAV(myOption.UseMP4box, videoPath, audioPath, audioMaterial, savePath,
int code = BBDownMuxer.MuxAV(myOption.UseMP4box, p.bvid, videoPath, audioPath, audioMaterial, savePath,
desc,
title,
p.ownerName ?? "",
Expand Down Expand Up @@ -753,7 +758,7 @@ private static async Task DownloadPageAsync(Page p, MyOption myOption, VInfo vIn
Log($"开始混流视频{(subtitleInfo.Any() ? "和字幕" : "")}...");
if (myOption.AudioOnly)
savePath = savePath[..^4] + ".m4a";
int code = BBDownMuxer.MuxAV(false, videoPath, "", audioMaterial, savePath,
int code = BBDownMuxer.MuxAV(false, p.bvid, videoPath, "", audioMaterial, savePath,
desc,
title,
p.ownerName ?? "",
Expand Down

0 comments on commit c5d97c2

Please sign in to comment.