From 27191240f467cead648a21aef5620bb59ed30bc6 Mon Sep 17 00:00:00 2001 From: nilaoda Date: Mon, 31 Oct 2022 23:54:56 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=B4=E6=92=AD=E5=BD=95=E5=88=B6=E5=88=B0?= =?UTF-8?q?=E8=BE=BE=E9=99=90=E5=88=B6=E5=90=8E=E5=85=81=E8=AE=B8=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=B7=B7=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DownloadManager/SimpleDownloadManager.cs | 22 +--------- .../SimpleLiveRecordManager2.cs | 43 ++++++++++++++++++- src/N_m3u8DL-RE/Util/OtherUtil.cs | 18 ++++++++ 3 files changed, 61 insertions(+), 22 deletions(-) diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs index e8be60c..3610847 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs @@ -49,24 +49,6 @@ namespace N_m3u8DL_RE.DownloadManager } } - //若该文件夹为空,删除,同时判断其父文件夹,直到遇到根目录或不为空的目录 - private void SafeDeleteDir(string dirPath) - { - if (string.IsNullOrEmpty(dirPath) || !Directory.Exists(dirPath)) - return; - - var parent = Path.GetDirectoryName(dirPath)!; - if (!Directory.EnumerateFileSystemEntries(dirPath).Any()) - { - Directory.Delete(dirPath); - } - else - { - return; - } - SafeDeleteDir(parent); - } - //从文件读取KEY private async Task SearchKeyAsync(string? currentKID) { @@ -532,7 +514,7 @@ namespace N_m3u8DL_RE.DownloadManager { File.Delete(file); } - SafeDeleteDir(tmpDir); + OtherUtil.SafeDeleteDir(tmpDir); } //重新读取init信息 @@ -650,7 +632,7 @@ namespace N_m3u8DL_RE.DownloadManager Logger.WarnMarkUp("[grey]Cleaning files...[/]"); OutputFiles.ForEach(f => File.Delete(f.FilePath)); var tmpDir = DownloaderConfig.MyOptions.TmpDir ?? Environment.CurrentDirectory; - SafeDeleteDir(tmpDir); + OtherUtil.SafeDeleteDir(tmpDir); } else Logger.ErrorMarkUp($"Mux failed"); //判断是否要改名 diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs index f26fd32..24af798 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs @@ -24,6 +24,7 @@ namespace N_m3u8DL_RE.DownloadManager DownloaderConfig DownloaderConfig; StreamExtractor StreamExtractor; List SelectedSteams; + List OutputFiles = new(); DateTime NowDateTime; DateTime? PublishDateTime; bool STOP_FLAG = false; @@ -533,6 +534,15 @@ namespace N_m3u8DL_RE.DownloadManager if (fileOutputStream != null) { + //记录所有文件信息 + OutputFiles.Add(new OutputFile() + { + Index = task.Id, + FilePath = fileOutputStream.Name, + LangCode = streamSpec.Language, + Description = streamSpec.Name, + Mediainfos = mediaInfos + }); fileOutputStream.Close(); fileOutputStream.Dispose(); } @@ -685,9 +695,38 @@ namespace N_m3u8DL_RE.DownloadManager var success = Results.Values.All(v => v == true); //混流 - if (success && DownloaderConfig.MyOptions.MuxAfterDone) + if (success && DownloaderConfig.MyOptions.MuxAfterDone && OutputFiles.Count > 0) { - Logger.Error("Not supported yet!"); + OutputFiles = OutputFiles.OrderBy(o => o.Index).ToList(); + if (DownloaderConfig.MyOptions.MuxImports != null) + { + OutputFiles.AddRange(DownloaderConfig.MyOptions.MuxImports); + } + OutputFiles.ForEach(f => Logger.WarnMarkUp($"[grey]{Path.GetFileName(f.FilePath).EscapeMarkup()}[/]")); + var saveDir = DownloaderConfig.MyOptions.SaveDir ?? Environment.CurrentDirectory; + var ext = DownloaderConfig.MyOptions.MuxToMp4 ? ".mp4" : ".mkv"; + var outName = $"{DownloaderConfig.MyOptions.SaveName ?? NowDateTime.ToString("yyyy-MM-dd_HH-mm-ss")}.MUX"; + var outPath = Path.Combine(saveDir, outName); + Logger.WarnMarkUp($"Muxing to [grey]{outName.EscapeMarkup()}{ext}[/]"); + var result = false; + if (DownloaderConfig.MyOptions.UseMkvmerge) result = MergeUtil.MuxInputsByMkvmerge(DownloaderConfig.MyOptions.MkvmergeBinaryPath!, OutputFiles.ToArray(), outPath); + else result = MergeUtil.MuxInputsByFFmpeg(DownloaderConfig.MyOptions.FFmpegBinaryPath!, OutputFiles.ToArray(), outPath, DownloaderConfig.MyOptions.MuxToMp4); + //完成后删除各轨道文件 + if (result && !DownloaderConfig.MyOptions.MuxKeepFiles) + { + Logger.WarnMarkUp("[grey]Cleaning files...[/]"); + OutputFiles.ForEach(f => File.Delete(f.FilePath)); + var tmpDir = DownloaderConfig.MyOptions.TmpDir ?? Environment.CurrentDirectory; + OtherUtil.SafeDeleteDir(tmpDir); + } + else Logger.ErrorMarkUp($"Mux failed"); + //判断是否要改名 + var newPath = Path.ChangeExtension(outPath, ext); + if (result && !File.Exists(newPath)) + { + Logger.WarnMarkUp($"Rename to [grey]{Path.GetFileName(newPath).EscapeMarkup()}[/]"); + File.Move(outPath + ext, newPath); + } } return success; diff --git a/src/N_m3u8DL-RE/Util/OtherUtil.cs b/src/N_m3u8DL-RE/Util/OtherUtil.cs index 0a22190..1c55278 100644 --- a/src/N_m3u8DL-RE/Util/OtherUtil.cs +++ b/src/N_m3u8DL-RE/Util/OtherUtil.cs @@ -118,5 +118,23 @@ namespace N_m3u8DL_RE.Util return new TimeSpan(days, hours, mins, secs); } + + //若该文件夹为空,删除,同时判断其父文件夹,直到遇到根目录或不为空的目录 + public static void SafeDeleteDir(string dirPath) + { + if (string.IsNullOrEmpty(dirPath) || !Directory.Exists(dirPath)) + return; + + var parent = Path.GetDirectoryName(dirPath)!; + if (!Directory.EnumerateFileSystemEntries(dirPath).Any()) + { + Directory.Delete(dirPath); + } + else + { + return; + } + SafeDeleteDir(parent); + } } }