From 87b069657d7a9d2f266e21febf960e9ded9ab37a Mon Sep 17 00:00:00 2001 From: nilaoda Date: Sat, 30 Nov 2024 00:20:29 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3mp4decrypt=E8=A7=A3=E5=AF=86?= =?UTF-8?q?=E4=B8=AD=E6=96=87=E6=96=87=E4=BB=B6=E5=90=8D=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20(#524)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/N_m3u8DL-RE.Common/Resource/ResString.cs | 3 +- src/N_m3u8DL-RE.Common/Resource/StaticText.cs | 6 ++++ src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs | 2 +- src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs | 35 ++++++++++++++----- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/N_m3u8DL-RE.Common/Resource/ResString.cs b/src/N_m3u8DL-RE.Common/Resource/ResString.cs index 4f4603ec..5d51837c 100644 --- a/src/N_m3u8DL-RE.Common/Resource/ResString.cs +++ b/src/N_m3u8DL-RE.Common/Resource/ResString.cs @@ -1,6 +1,6 @@ namespace N_m3u8DL_RE.Common.Resource; -public class ResString +public static class ResString { public static string CurrentLoc = "en-US"; @@ -125,6 +125,7 @@ public class ResString public static string promptTitle => GetText("promptTitle"); public static string readingInfo => GetText("readingInfo"); public static string searchKey => GetText("searchKey"); + public static string decryptionFailed => GetText("decryptionFailed"); public static string segmentCountCheckNotPass => GetText("segmentCountCheckNotPass"); public static string selectedStream => GetText("selectedStream"); public static string startDownloading => GetText("startDownloading"); diff --git a/src/N_m3u8DL-RE.Common/Resource/StaticText.cs b/src/N_m3u8DL-RE.Common/Resource/StaticText.cs index 09220302..bc69d059 100644 --- a/src/N_m3u8DL-RE.Common/Resource/StaticText.cs +++ b/src/N_m3u8DL-RE.Common/Resource/StaticText.cs @@ -910,6 +910,12 @@ internal class StaticText zhTW: "正在嘗試從文本文件搜尋KEY...", enUS: "Trying to search for KEY from text file..." ), + ["decryptionFailed"] = new TextContainer + ( + zhCN: "解密失败", + zhTW: "解密失敗", + enUS: "Decryption failed" + ), ["segmentCountCheckNotPass"] = new TextContainer ( zhCN: "分片数量校验不通过, 共{}个,已下载{}.", diff --git a/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs b/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs index 7afc33df..1bd9e001 100644 --- a/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs +++ b/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs @@ -17,7 +17,7 @@ namespace N_m3u8DL_RE.CommandLine; internal partial class CommandInvoker { - public const string VERSION_INFO = "N_m3u8DL-RE (Beta version) 20241129"; + public const string VERSION_INFO = "N_m3u8DL-RE (Beta version) 20241130"; [GeneratedRegex("((best|worst)\\d*|all)")] private static partial Regex ForStrRegex(); diff --git a/src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs b/src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs index 549eb913..5510fe1d 100644 --- a/src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs +++ b/src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs @@ -17,6 +17,9 @@ public static async Task DecryptAsync(DecryptEngine decryptEngine, string var keyPairs = keys.ToList(); string? keyPair = null; string? trackId = null; + string? tmpEncFile = null; + string? tmpDecFile = null; + string? workDir = null; if (isMultiDRM) { @@ -79,7 +82,12 @@ public static async Task DecryptAsync(DecryptEngine decryptEngine, string { cmd += $" --fragments-info \"{init}\" "; } - cmd += $" \"{source}\" \"{dest}\""; + // 解决mp4decrypt中文问题 切换到源文件所在目录并改名再解密 + workDir = Path.GetDirectoryName(source)!; + tmpEncFile = Path.Combine(workDir, $"{Guid.NewGuid()}{Path.GetExtension(source)}"); + tmpDecFile = Path.Combine(workDir, $"{Path.GetFileNameWithoutExtension(tmpEncFile)}_dec{Path.GetExtension(tmpEncFile)}"); + File.Move(source, tmpEncFile); + cmd += $" \"{Path.GetFileName(tmpEncFile)}\" \"{Path.GetFileName(tmpDecFile)}\""; } else { @@ -95,30 +103,41 @@ public static async Task DecryptAsync(DecryptEngine decryptEngine, string cmd = $"-loglevel error -nostdin -decryption_key {keyPair.Split(':')[1]} -i \"{enc}\" -c copy \"{dest}\""; } - await RunCommandAsync(bin, cmd); + var isSuccess = await RunCommandAsync(bin, cmd, workDir); + + // mp4decrypt 还原文件改名操作 + if (workDir is not null) + { + if (File.Exists(tmpEncFile)) File.Move(tmpEncFile, source); + if (File.Exists(tmpDecFile)) File.Move(tmpDecFile, dest); + } - if (File.Exists(dest) && new FileInfo(dest).Length > 0) + if (isSuccess) { if (tmpFile != "" && File.Exists(tmpFile)) File.Delete(tmpFile); return true; } - + + Logger.Error(ResString.decryptionFailed); return false; } - private static async Task RunCommandAsync(string name, string arg) + private static async Task RunCommandAsync(string name, string arg, string? workDir = null) { Logger.DebugMarkUp($"FileName: {name}"); Logger.DebugMarkUp($"Arguments: {arg}"); - await Process.Start(new ProcessStartInfo() + var process = Process.Start(new ProcessStartInfo() { FileName = name, Arguments = arg, // RedirectStandardOutput = true, // RedirectStandardError = true, CreateNoWindow = true, - UseShellExecute = false - })!.WaitForExitAsync(); + UseShellExecute = false, + WorkingDirectory = workDir + }); + await process!.WaitForExitAsync(); + return process.ExitCode == 0; } ///