From 3c17a2044799250e43637126c556c0c0c01022d6 Mon Sep 17 00:00:00 2001 From: nilaoda Date: Sun, 21 Aug 2022 22:23:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=90=88=E5=B9=B6=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E5=88=A4=E6=96=AD=E5=92=8C=E5=88=A0=E9=99=A4=E4=B8=B4?= =?UTF-8?q?=E6=97=B6=E6=96=87=E4=BB=B6=E5=A4=B9=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DownloadManager/SimpleDownloadManager.cs | 32 +++++++++++++------ src/N_m3u8DL-RE/Util/MergeUtil.cs | 3 ++ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs index 39d3fad..249d13e 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs @@ -48,6 +48,24 @@ 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) { @@ -254,8 +272,8 @@ namespace N_m3u8DL_RE.DownloadManager //修改输出后缀 var outputExt = "." + streamSpec.Extension; if (streamSpec.Extension == null) outputExt = ".ts"; - else if (streamSpec.MediaType == MediaType.AUDIO) outputExt = ".m4a"; - else if (streamSpec.MediaType != MediaType.SUBTITLES) outputExt = ".mp4"; + else if (streamSpec.MediaType == MediaType.AUDIO && streamSpec.Extension == "m4s") outputExt = ".m4a"; + else if (streamSpec.MediaType != MediaType.SUBTITLES && streamSpec.Extension == "m4s") outputExt = ".mp4"; var output = Path.Combine(saveDir, saveName + outputExt); @@ -489,10 +507,7 @@ namespace N_m3u8DL_RE.DownloadManager { File.Delete(file); } - if (!Directory.EnumerateFiles(tmpDir).Any()) - { - Directory.Delete(tmpDir); - } + SafeDeleteDir(tmpDir); } //重新读取init信息 @@ -576,10 +591,7 @@ namespace N_m3u8DL_RE.DownloadManager { OutputFiles.ForEach(f => File.Delete(f.FilePath)); var tmpDir = DownloaderConfig.TmpDir ?? Environment.CurrentDirectory; - if (!Directory.EnumerateFiles(tmpDir).Any()) - { - Directory.Delete(tmpDir); - } + SafeDeleteDir(tmpDir); } else Logger.ErrorMarkUp($"Mux failed"); } diff --git a/src/N_m3u8DL-RE/Util/MergeUtil.cs b/src/N_m3u8DL-RE/Util/MergeUtil.cs index bca9fe6..d379c0d 100644 --- a/src/N_m3u8DL-RE/Util/MergeUtil.cs +++ b/src/N_m3u8DL-RE/Util/MergeUtil.cs @@ -79,6 +79,9 @@ namespace N_m3u8DL_RE.Util bool writeDate = true, string poster = "", string audioName = "", string title = "", string copyright = "", string comment = "", string encodingTool = "", string recTime = "") { + //改为绝对路径 + outputPath = Path.GetFullPath(outputPath); + string dateString = string.IsNullOrEmpty(recTime) ? DateTime.Now.ToString("o") : recTime; StringBuilder command = new StringBuilder("-loglevel warning -i concat:\"");