From 164f2cb59b14bcd7ee6f9d7e4851a4badc5162bf Mon Sep 17 00:00:00 2001 From: nilaoda Date: Sun, 20 Oct 2024 21:41:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81TS=E6=A0=BC=E5=BC=8F=E6=B7=B7?= =?UTF-8?q?=E6=B5=81=20(#466)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs | 7 +++--- .../DownloadManager/SimpleDownloadManager.cs | 4 ++-- .../SimpleLiveRecordManager2.cs | 4 ++-- src/N_m3u8DL-RE/Entity/MuxOptions.cs | 8 ++----- src/N_m3u8DL-RE/Enum/MuxFormat.cs | 8 +++++++ src/N_m3u8DL-RE/Enum/SubtitleFormat.cs | 17 +++++--------- src/N_m3u8DL-RE/Util/MergeUtil.cs | 22 ++++++++----------- src/N_m3u8DL-RE/Util/OtherUtil.cs | 11 ++++++++++ 8 files changed, 43 insertions(+), 38 deletions(-) create mode 100644 src/N_m3u8DL-RE/Enum/MuxFormat.cs diff --git a/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs b/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs index e9311c4..fbb78b8 100644 --- a/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs +++ b/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs @@ -18,7 +18,7 @@ namespace N_m3u8DL_RE.CommandLine { internal partial class CommandInvoker { - public const string VERSION_INFO = "N_m3u8DL-RE (Beta version) 20240630"; + public const string VERSION_INFO = "N_m3u8DL-RE (Beta version) 20241020"; [GeneratedRegex("((best|worst)\\d*|all)")] private static partial Regex ForStrRegex(); @@ -438,7 +438,8 @@ namespace N_m3u8DL_RE.CommandLine var p = new ComplexParamParser(v); //混流格式 var format = p.GetValue("format") ?? v.Split(':')[0]; //若未获取到,直接:前的字符串作为format解析 - if (format != "mp4" && format != "mkv") + var parseResult = System.Enum.TryParse(format.ToUpperInvariant(), out MuxFormat muxFormat); + if (!parseResult) { result.ErrorMessage = $"format={format} not valid"; return null; @@ -480,7 +481,7 @@ namespace N_m3u8DL_RE.CommandLine return new MuxOptions() { UseMkvmerge = muxer == "mkvmerge", - MuxToMp4 = format == "mp4", + MuxFormat = muxFormat, KeepFiles = keep == "true", SkipSubtitle = skipSub == "true", BinPath = bin_path == "auto" ? null : bin_path diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs index 5893cc5..118b86a 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs @@ -725,14 +725,14 @@ namespace N_m3u8DL_RE.DownloadManager } OutputFiles.ForEach(f => Logger.WarnMarkUp($"[grey]{Path.GetFileName(f.FilePath).EscapeMarkup()}[/]")); var saveDir = DownloaderConfig.MyOptions.SaveDir ?? Environment.CurrentDirectory; - var ext = DownloaderConfig.MyOptions.MuxOptions.MuxToMp4 ? ".mp4" : ".mkv"; + var ext = OtherUtil.GetMuxExtension(DownloaderConfig.MyOptions.MuxOptions.MuxFormat); var dirName = Path.GetFileName(DownloaderConfig.DirPrefix); var outName = $"{dirName}.MUX"; var outPath = Path.Combine(saveDir, outName); Logger.WarnMarkUp($"Muxing to [grey]{outName.EscapeMarkup()}{ext}[/]"); var result = false; if (DownloaderConfig.MyOptions.MuxOptions.UseMkvmerge) result = MergeUtil.MuxInputsByMkvmerge(DownloaderConfig.MyOptions.MkvmergeBinaryPath!, OutputFiles.ToArray(), outPath); - else result = MergeUtil.MuxInputsByFFmpeg(DownloaderConfig.MyOptions.FFmpegBinaryPath!, OutputFiles.ToArray(), outPath, DownloaderConfig.MyOptions.MuxOptions.MuxToMp4, !DownloaderConfig.MyOptions.NoDateInfo); + else result = MergeUtil.MuxInputsByFFmpeg(DownloaderConfig.MyOptions.FFmpegBinaryPath!, OutputFiles.ToArray(), outPath, DownloaderConfig.MyOptions.MuxOptions.MuxFormat, !DownloaderConfig.MyOptions.NoDateInfo); //完成后删除各轨道文件 if (result) { diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs index 92674dc..80e62e0 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs @@ -896,14 +896,14 @@ namespace N_m3u8DL_RE.DownloadManager } OutputFiles.ForEach(f => Logger.WarnMarkUp($"[grey]{Path.GetFileName(f.FilePath).EscapeMarkup()}[/]")); var saveDir = DownloaderConfig.MyOptions.SaveDir ?? Environment.CurrentDirectory; - var ext = DownloaderConfig.MyOptions.MuxOptions.MuxToMp4 ? ".mp4" : ".mkv"; + var ext = OtherUtil.GetMuxExtension(DownloaderConfig.MyOptions.MuxOptions.MuxFormat); var dirName = Path.GetFileName(DownloaderConfig.DirPrefix); var outName = $"{dirName}.MUX"; var outPath = Path.Combine(saveDir, outName); Logger.WarnMarkUp($"Muxing to [grey]{outName.EscapeMarkup()}{ext}[/]"); var result = false; if (DownloaderConfig.MyOptions.MuxOptions.UseMkvmerge) result = MergeUtil.MuxInputsByMkvmerge(DownloaderConfig.MyOptions.MkvmergeBinaryPath!, OutputFiles.ToArray(), outPath); - else result = MergeUtil.MuxInputsByFFmpeg(DownloaderConfig.MyOptions.FFmpegBinaryPath!, OutputFiles.ToArray(), outPath, DownloaderConfig.MyOptions.MuxOptions.MuxToMp4, !DownloaderConfig.MyOptions.NoDateInfo); + else result = MergeUtil.MuxInputsByFFmpeg(DownloaderConfig.MyOptions.FFmpegBinaryPath!, OutputFiles.ToArray(), outPath, DownloaderConfig.MyOptions.MuxOptions.MuxFormat, !DownloaderConfig.MyOptions.NoDateInfo); //完成后删除各轨道文件 if (result) { diff --git a/src/N_m3u8DL-RE/Entity/MuxOptions.cs b/src/N_m3u8DL-RE/Entity/MuxOptions.cs index 7d43757..5e27134 100644 --- a/src/N_m3u8DL-RE/Entity/MuxOptions.cs +++ b/src/N_m3u8DL-RE/Entity/MuxOptions.cs @@ -1,15 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using N_m3u8DL_RE.Enum; namespace N_m3u8DL_RE.Entity { internal class MuxOptions { public bool UseMkvmerge { get; set; } = false; - public bool MuxToMp4 { get; set; } = false; + public MuxFormat MuxFormat { get; set; } = MuxFormat.MP4; public bool KeepFiles { get; set; } = false; public bool SkipSubtitle { get; set; } = false; public string? BinPath { get; set; } diff --git a/src/N_m3u8DL-RE/Enum/MuxFormat.cs b/src/N_m3u8DL-RE/Enum/MuxFormat.cs new file mode 100644 index 0000000..cfff6ec --- /dev/null +++ b/src/N_m3u8DL-RE/Enum/MuxFormat.cs @@ -0,0 +1,8 @@ +namespace N_m3u8DL_RE.Enum; + +internal enum MuxFormat +{ + MP4, + MKV, + TS, +} \ No newline at end of file diff --git a/src/N_m3u8DL-RE/Enum/SubtitleFormat.cs b/src/N_m3u8DL-RE/Enum/SubtitleFormat.cs index e1c75e7..cac8171 100644 --- a/src/N_m3u8DL-RE/Enum/SubtitleFormat.cs +++ b/src/N_m3u8DL-RE/Enum/SubtitleFormat.cs @@ -1,14 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace N_m3u8DL_RE.Enum; -namespace N_m3u8DL_RE.Enum +internal enum SubtitleFormat { - internal enum SubtitleFormat - { - VTT, - SRT - } -} + VTT, + SRT +} \ No newline at end of file diff --git a/src/N_m3u8DL-RE/Util/MergeUtil.cs b/src/N_m3u8DL-RE/Util/MergeUtil.cs index 79d8260..92b325c 100644 --- a/src/N_m3u8DL-RE/Util/MergeUtil.cs +++ b/src/N_m3u8DL-RE/Util/MergeUtil.cs @@ -1,16 +1,9 @@ using N_m3u8DL_RE.Common.Log; using N_m3u8DL_RE.Entity; using Spectre.Console; -using System; -using System.Collections.Generic; -using System.CommandLine; using System.Diagnostics; -using System.Globalization; -using System.Linq; using System.Text; -using System.Threading.Tasks; -using System.Xml; -using System.Xml.Linq; +using N_m3u8DL_RE.Enum; namespace N_m3u8DL_RE.Util { @@ -186,9 +179,9 @@ namespace N_m3u8DL_RE.Util return code == 0; } - public static bool MuxInputsByFFmpeg(string binary, OutputFile[] files, string outputPath, bool mp4, bool dateinfo) + public static bool MuxInputsByFFmpeg(string binary, OutputFile[] files, string outputPath, MuxFormat muxFormat, bool dateinfo) { - var ext = mp4 ? "mp4" : "mkv"; + var ext = OtherUtil.GetMuxExtension(muxFormat); string dateString = DateTime.Now.ToString("o"); StringBuilder command = new StringBuilder("-loglevel warning -nostdin -y -dn "); @@ -206,10 +199,13 @@ namespace N_m3u8DL_RE.Util var srt = files.Any(x => x.FilePath.EndsWith(".srt")); - if (mp4) + if (muxFormat == MuxFormat.MP4) command.Append($" -strict unofficial -c:a copy -c:v copy -c:s mov_text "); //mp4不支持vtt/srt字幕,必须转换格式 - else + else if (muxFormat == MuxFormat.TS) + command.Append($" -strict unofficial -c:a copy -c:v copy "); + else if (muxFormat == MuxFormat.MKV) command.Append($" -strict unofficial -c:a copy -c:v copy -c:s {(srt ? "srt" : "webvtt")} "); + else throw new ArgumentException($"unknown format: {muxFormat}"); //CLEAN command.Append(" -map_metadata -1 "); @@ -254,7 +250,7 @@ namespace N_m3u8DL_RE.Util if (dateinfo) command.Append($" -metadata date=\"{dateString}\" "); command.Append($" -ignore_unknown -copy_unknown "); - command.Append($" \"{outputPath}.{ext}\""); + command.Append($" \"{outputPath}{ext}\""); var code = InvokeFFmpeg(binary, command.ToString(), Environment.CurrentDirectory); diff --git a/src/N_m3u8DL-RE/Util/OtherUtil.cs b/src/N_m3u8DL-RE/Util/OtherUtil.cs index e8afee5..6435160 100644 --- a/src/N_m3u8DL-RE/Util/OtherUtil.cs +++ b/src/N_m3u8DL-RE/Util/OtherUtil.cs @@ -163,5 +163,16 @@ namespace N_m3u8DL_RE.Util { return Environment.GetEnvironmentVariable(key) ?? defaultValue; } + + public static string GetMuxExtension(MuxFormat muxFormat) + { + return muxFormat switch + { + MuxFormat.MP4 => ".mp4", + MuxFormat.MKV => ".mkv", + MuxFormat.TS => ".ts", + _ => throw new ArgumentException($"unknown format: {muxFormat}") + }; + } } }